All ITKnowledge Source Code Search Tips Advanced Search |

| Mastering Java Threads by Marc Adler and David Herst DDC Publishing, Inc. ISBN: 1562438425 Pub Date: 05/01/99

Search this book:

Previous | Table of Contents | Next

#### The wait[] & notify[] Methods

The methods wait(), notify(), and notifyAll() are used to control synchronization within Java applications. Note that these methods are part of Java’s Object class, not the Thread class.

There are three forms associated with the wait() method, as listed below:

  1. public final void wait() throws InterruptedException
  2. public final void wait(long timeout) throws InterruptedException
  3. public final void wait(long timeout, int nanos) throws InterruptedException

Note the following points regarding the wait() and notify() methods.

  • The first form of wait() will wait forever; the other two forms will wait for a particular period of time.
  • The wait() method will cause a thread to wait until it is notified or the specified timeout expires. It can only be called from within a synchronized method by the thread that holds the lock. The wait() method will also release the monitor.

  | A thread will awaken if the notify() or notifyAll() methods are called.

  • The notify() method notifies a single waiting thread of a change in condition of another thread. The thread effecting the change notifies the waiting thread using notify(). Threads that want to wait for a condition to change before proceeding can call wait().
  • The notifyAll() method notifies all threads waiting for a condition to change. Threads that are waiting are generally waiting for another thread to change some condition. Thus, the thread effecting a change for which more than one thread is waiting notifies all of the waiting threads using the method notifyAll(). One of the waiting threads will grab the monitor and proceed. The other waiting threads will be forced to wait longer.
  • The notify() and notifyAll() methods can only be called from within a synchronized method by the thread that holds the lock.

  | Most often, you should use notifyAll() over the notify() method.

Producer & Consumer Interaction

Table 9-1 implements a timeline so the interaction between the Producer and Consumer with the monitor can be seen.

Table 9-1: Interaction of the Producer and the Consumer with the monitor
Time | Consumer | Producer
--- | --- | ---
T1 | Run() calls get() and the Consumer acquires the monitor when it enters Get(). | Polling thread pulls some stock info off of the server.
T2 |  | Thread calls put(), but because the Consumer has the monitor, the Producer is forced to sleep at the entrance to put().
T3 | No stock available, so calls wait(). which releases the monitor | Locked out...
T4 | waiting... | Wakes up and enters put(). The Producer now has the monitor.
T5 | waiting... | Puts stock information into a Stock object.
T6 | waiting... | Sets m_bStockPending to true
T7 | waiting... | Calls notifyAll()
T8 | Woken and waiting for its time slice | Exits put() and releases the monitor
T9 | Gets out of the while loop. Reacquires the monitor. | Polls for another stock. it is locked out of put() until the Consumer finishes.
T10 | Fetches the stock object and inserts it into the database. | Gets another stock and calls put().
T11 |  | Locked out of put until the monitor is released
T12 | Calls notifyAll() and exits get(). The monitor is released. |
T13 |  | Enters put() and acquires the monitor.
T14 | Run() calls get(), but the Consumer is locked out. |

### Another Example of Synchronization

The following example is by Chuck McManis, as published in Java World Magazine1. It is a simple simulation of a ping pong game involving two players, Bob and Alice. The shared resource is a ping pong table, which is represented by the Java object named PingPong.

> 1April 1996 issue.

#### How the Program Starts

The program starts off by creating a PingPong object, and then creates two players, each of which plays the game in a separate thread. After sleeping for 5 seconds, the game is terminated.

The Player object consists of a pointer to the PingPong table object and the name of the player’s opponent. The Player’s run() method loops continuously, calling PingPong.hit() until the PingPong’s hit method returns false.

#### Guts of the Program

The real guts of the program are in the PingPong method. You can consider the “ball” as being the critical shared resource; only one player can hit the ball at a time. The action of hitting the “ball” is represented by the following statement:

             System.out.println(“PING! (“+x+”) ”);

Only one player can reach this code at a time. More importantly, a thread cannot execute this statement twice in a row; this statement must be executed alternatively by thread one and thread two.

Previous | Table of Contents | Next

Copyright © DDC Publishing

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.