0% found this document useful (0 votes)
45 views2 pages

Wait/notify: Synchronized Code To Ensure That The Current Code Owns The Monitor

Wait and notify allow threads to communicate and synchronize access to shared resources, with wait causing a thread to release a lock and go to sleep until another thread calls notify to wake it up, as wait and notify must be used within synchronized code to ensure the calling thread owns the monitor. Thread scheduling and priorities can vary between implementations so one cannot rely on a particular thread being awakened by notify and should generally use notifyAll to wake all waiting threads.

Uploaded by

dipen12
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Wait/notify: Synchronized Code To Ensure That The Current Code Owns The Monitor

Wait and notify allow threads to communicate and synchronize access to shared resources, with wait causing a thread to release a lock and go to sleep until another thread calls notify to wake it up, as wait and notify must be used within synchronized code to ensure the calling thread owns the monitor. Thread scheduling and priorities can vary between implementations so one cannot rely on a particular thread being awakened by notify and should generally use notifyAll to wake all waiting threads.

Uploaded by

dipen12
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

wait/notify In addition to having a lock that can be grabbed and released, each object has a system that allows

it to pause or wait whilst another thread takes over the lock. This allows Threads to communicate the condition of readiness to execute. Because of the single inheritance nature of Java, every object is a child of the great grand ancestor Object class from which it gets this Thread communication capability. wait and notify should be placed within synchronized code to ensure that the current code owns the monitor

A call to wait from within synchronized code causes the thread to give up its lock and go to sleep. This normally happens to allow another thread to obtain the lock and continue some processing. The wait method is meaningless without the use of notify or notifyAll which allows code that is waiting to be notified that it can wake up and continue executing. A typical example of using the wait/notify protocol to allow communication between Threads appears to involve apparently endless loops such as
//producing code while(true){ try{ wait(); }catch (InterruptedException e) {} } //some producing action goes here notifyAll();

As true is notorious for staying true this, code looks at first glance like it will just loop forever. The wait method however effectively means give up the lock on the object and wait until the notify or notifyAll method tells you to wake up.

Thread scheduling is implementation dependent

and cannot be relied on to act the same way on every JVM

Unlike most aspects of Java, Threading does not act the same on different platforms. Two areas of difference are Thread scheduling and Thread priorities. The two approaches to scheduling are Preemptive Time slicing In a pre-emptive system one program can "pre-empt" another to get its share of CPU time. In a time sliced system each thread gets a "slice" of the CPU time and then gets moved to the ready state. This ensures against a single thread getting all of the CPU time. The downside is that you cannot be certain how long a Thread might execute or even when it will be running. Although Java defines priorities for threads from the lowest at 1 to the highest at 10, some platforms will accurately recognise these priorities whereas others will not. The notify method will wake up one thread waiting to reacquire the monitor for the object. You cannot be certain which thread gets woken. If you have only one waiting thread then you do not have a problem. If you have multiple waiting threads then it will probably the thread that has been waiting the longest that will wake up. However you cannot be certain, and the priorities of the threads will influence the result. As a result you are generally advised to usenotifyAll instead of notify, and not to make assumptions about scheduling or priorities. Of course this is not always possible and you may have to try to test your code on as many platforms as possible.

You might also like