Inter thread Communication
The preceding examples unconditionally blocked other threads from asynchronous access to certain methods. This use of the implicit monitors in Java objects is powerful. but you can achieve a more subtle level of control through interprocess communication. As you will see, this is especially easy in Java. As discussed earlier, multithreading replaces event loop programming by dividing your tasks into discrete and logical units. Threads also provide a secondary benefit: they do away with polling. Polling is usually implemented by a loop that is used to check some condition repeatedly, Once the condition is true, appropriate action is taken. This wastes O'U time. For example, consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wail until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waited for the producer 10 produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, [ava includes an elegant interprocess communication mechanism via the wait(), notify(), and notify ALL( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method. Although conceptually adv.mced Irom computer science perspective, the rules for using these methods are actually quite simple wait() tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ). notify() wakes lip the first thread that culled waitt ) on the same object. D notify All () wakes up all the threads that called wait() on the same object. The highest priority thread will run first.
These methods are declared within Object, as shown here: final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )
Additional forms of wait( ) exist that allow you to specify a period of time to wait. The following sample prograin incorrectly implements a simple form of the producer/consumer problem. It consists 'of four classes: Q, the queue that you're trying to synchronize; Producer, the threaded object that is producing queue entries Consumer, the threaded object that is consuming queue entries; 'and C, the tiny class that creates the single Q, Producer, and Consumer. Although the put( ) and get( ) methods on Q are synchronized, nothing stops the producer from overrunning the consumer, nor will anything stop the consumer from consuming, the same queue value twice. Thus, you get the erroneous output shown here (the exact output will vary with processor speed and task load):
As you can see, after the producer put 1, the consumer started and got the same five times in a row. Then, the producer resumed and produced 2 through 7 without letting. the consumer have a chance to consume them. The proper way to write this program in Java is to use wait ) and notify( ) to signal in both directions, as shown here:
Inside get waite ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready. When this happens, execution inside get( ) resumes. 'After the data has been obtained, get() calls notify( ). nus tells Producer that it is okay to put more data in the queue. Inside put(), wait() suspends execution until the Consumer has removed the item from the queue. ,When execution resumes, the next item of datainput in the queue, and p.otify() is called. This tells the Consumer that it should now remove it . Here is some out from this program, which shows the clean synchronous behavior: