Java Multitasking  «Prev 

Checking in and out enqueue thread

In this example, we have issued a call to notify(), because we know that there is only one Java thread waiting which is the emcee. But if there might have been more than one, the safer call would have been to notifyAll(). While notify() starts a waiting thread, if there is more than one waiting thread, you cannot be certain which thread will wake up. A call to notifyAll() wakes up all waiting threads. SynchronizedQueue, which represents the podium, provides three methods used by the citizens. Here they are:
The first of the three additional SynchronizedQueue methods allows a speaker to check in and register with the podium.

public synchronized void checkIn () { 
  started = true; 
  count++; 
} 

The second adds utterances (String instances) to the queue.
public synchronized void enqueue(Object elt) { 
 super.addElement(elt); 
 notify(); 
} 

The third allows the speaker to check out.
public synchronized void checkOut () { 
 count--; 
 notify(); 
} 

Checking in and out is important for the SynchronizedQueue, because this allows it to determine whether there is anything left to be said.