Concepts of Programming Languages

(Sean Pound) #1
13.7 Java Threads 609

object has a wait list of all of the threads that have called wait on the object.
The notify method is called to tell one waiting thread that an event that it
may have been waiting for has occurred. The specific thread that is awakened
by notify cannot be determined, because the Java Virtual Machine ( JVM)
chooses one from the wait list of the thread object at random. Because of
this, along with the fact that the waiting threads may be waiting for different
conditions, the notifyAll method is often used, rather than notify. The
notifyAll method awakens all of the threads on the object’s wait list by put-
ting them in the task ready queue.
The methods wait, notify, and notifyAll can be called only from
within a synchronized method, because they use the lock placed on an object by
such a method. The call to wait is always put in a while loop that is controlled
by the condition for which the method is waiting. The while loop is necessary
because the notify or notifyAll that awakened the thread may have been
called because of a change in a condition other than the one for which the thread
was waiting. If it was a call to notifyAll, there is even a smaller chance that the
condition being waited for is now true. Because of the use of notifyAll, some
other thread may have changed the condition to false since it was last tested.
The wait method can throw InterruptedException, which is a
descendant of Exception. Java’s exception handling is discussed in Chapter



  1. Therefore, any code that calls wait must also catch InterruptedExcep-
    tion. Assuming the condition being waited for is called theCondition, the
    conventional way to use wait is as follows:


try {
while (!theCondition)
wait();
-- Do whatever is needed after theCondition comes true
}
catch(InterruptedException myProblem) {... }


The following program implements a circular queue for storing int val-
ues. It illustrates both cooperation and competition synchronization.


// Queue
// This class implements a circular queue for storing int
// values. It includes a constructor for allocating and
// initializing the queue to a specified size. It has
// synchronized methods for inserting values into and
// removing values from the queue.


class Queue {
private int [] que;
private int nextIn,
nextOut,
filled,
queSize;

Free download pdf