40 likes | 134 Views
Synchronized Threads and Threads Coordination. Run the program in syncPackage . This program creates 2 threads, which work on the same instance of CommandClass . This results in a race condition, which is usually bad.
E N D
Run the program in syncPackage. • This program creates 2 threads, which work on the same instance of CommandClass. This results in a race condition, which is usually bad. • Notice the CommandClass, the synchronized(this){} block is the region that only one thread can have access to at anytime. • What happens if you delete synchronized(this) and its curly braces? Synchronized Threads
In package main. • Each vehicle is controlled by a thread. • These 3 threads share a common instance of clearance manager. • Notice the wait() call (in method waitforproceed) in clearance manager. This function call will temporally put the current thread(the thread that made the call) into waiting state and let other threads run. The thread can wake up if other thread make the notify call (in method proceed and proceedAll) Threads Coordination
If there are multiple threads waiting for notify(), each time notify will wake up a single thread in the waiting queue. Queue is a first in, first out data structure. • notifyAll will wake up all threads in the waiting queue.