Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 11: Multithreaded Programming 243


system, the consumer would waste many CPU cycles while it waited for the producer to
produce. Once the producer was finished, it would start polling, wasting more CPU cycles
waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via
thewait( ),notify( ), andnotifyAll( )methods. These methods are implemented asfinal
methods inObject, so all classes have them. All three methods can be called only from
within asynchronizedcontext. Although conceptually advanced from a 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 callsnotify( ).

  • notify( )wakes up a thread that calledwait( )on the same object.

  • notifyAll( )wakes up all the threads that calledwait( )on the same object. One of
    the threads will be granted access.


These methods are declared withinObject, as shown here:


final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )

Additional forms ofwait( )exist that allow you to specify a period of time to wait.
Before working through an example that illustrates interthread communication, an
important point needs to be made. Althoughwait( )normally waits untilnotify( )or
notifyAll( )is called, there is a possibility that in very rare cases the waiting thread could be
awakened due to aspurious wakeup. In this case, a waiting thread resumes withoutnotify( )
ornotifyAll( )having been called. (In essence, the thread resumes for no apparent reason.)
Because of this remote possibility, Sun recommends that calls towait( )should take place
within a loop that checks the condition on which the thread is waiting. The following
example shows this technique.
Let’s now work through an example that useswait( )andnotify( ). To begin, consider
the following sample program that 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; andPC, the tiny class that creates the singleQ,
Producer, andConsumer.


// An incorrect implementation of a producer and consumer.
class Q {
int n;


synchronized int get() {
System.out.println("Got: " + n);
return n;
}

synchronized void put(int n) {
this.n = n;
System.out.println("Put: " + n);
}
}

Free download pdf