1 / 4

Synchronized Threads and Threads Coordination

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.

tadita
Download Presentation

Synchronized Threads and Threads Coordination

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Synchronized Threads and Threads Coordination

  2. 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

  3. 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

  4. 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.

More Related