Содержание
- 2. Synchronization Threads communicate primarily by sharing access to fields and the objects reference fields refer to.
- 3. Thread Interference Example class Counter { private int c = 0; public void increment() { c++;
- 4. Thread Interference I Interference happens when two operations, running in different threads, but acting on the
- 5. Thread Interference II Thread A: Retrieve c. Thread B: Retrieve c. Thread A: Increment retrieved value;
- 6. Thread Interference III Thread A's result is lost, overwritten by Thread B. Because they are unpredictable,
- 7. Exercise: Thread Inference Modify 511DepoSum project as follows: Create new method add100(int index) in the ListDepo
- 8. Exercise: Thread Inference See 821Unsync project for the full text. * InfopulseTraining Center
- 9. Synchronized Methods I public class SynchronizedCounter { private int c = 0; public synchronized void increment()
- 10. Synchronized Methods II When one thread is executing a synchronized method for an object, all other
- 11. Exercise: Synchronization Modify 821Unsync project using synchronized add100 method and check result. * InfopulseTraining Center
- 12. Constructor Synchronization Constructors cannot be synchronized — using the synchronized keyword with a constructor is a
- 13. Intrinsic Locks and Synchronization When a task wishes to execute a piece of code guarded by
- 14. Intrinsic Locks If a task is in a call to one of the synchronized methods, all
- 15. Concurrency Class Fields Especially important to make fields private when working with concurrency Otherwise the synchronized
- 16. Synchronized Statements Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:
- 17. Cooperation Between Tasks How to make tasks cooperate with each other, so that multiple tasks can
- 18. wait() Method wait( ) allows you to wait for a change in some condition that is
- 19. Don't do this! public void guardedJoy() { // Simple loop guard. Wastes // processor time. Don't
- 20. wait() Example (1 of 2) public synchronized guardedJoy() { while(!joy) { try { wait(); } catch
- 21. notify() / notifyAll() Methods wait( ) suspends the task while waiting for the world to change
- 22. wait() Example (2 of 2) public synchronized notifyJoy() { joy = true; notifyAll(); } * InfopulseTraining
- 23. Deadlock Deadlock describes a situation where two or more threads are blocked forever, waiting for each
- 25. Скачать презентацию