Java Multitasking  «Prev 

Masters of Ceremony Process at the Townhall

Java run() method

The MC instance asks the podium for a comment to be heard. Here is how:
The podium provides a method called dequeue(), which returns a String instance representing one of the citizens's utterances. The dequeue() method ensures that one utterance is returned at a time. After all the words of the citizens have been heard, the thread of the MC comes to an end and the town hall closes. Here's what the run() method for the MC looks like (you will see the dequeue() method in a moment):

public void run() {  
 Object utterance;  
 System.out.println("MC here: good morning.");  
 utterance = podium.dequeue();  
 while (utterance != null) {  
   System.out.println(utterance);  
   utterance = podium.dequeue();  
 }   
 System.out.println("MC here: good night.");  
}  

Even though you would usually avoid writing infinite loops like you would avoid the plague, infinite loops often appear in run() methods.
That way, the thread (such as an animation) runs forever, that is, until the thread itself is told to pause or halt permanently. In the town hall example, the run() method continues to loop as long as there are more utterances to be heard. Once all the citizens have spoken and their utterances have been heard, the run() method will return, thereby halting the thread.

Java Concurrency