Java Thread: notify() and wait() examples
This article contains two code examples to demonstrate Java concurrency. They stand for very typical usage. By understanding them, you will have a better understanding about notify() and wait().
1. Some background knowledge
synchronized
keyword is used for exclusive accessing.- To make a method synchronized, simply add the synchronized keyword to its declaration. Then no two invocations of synchronized methods on the same object can interleave with each other.
- Synchronized statements must specify the object that provides the intrinsic lock. When synchronized(this) is used, you have to avoid to synchronizing invocations of other objects' methods.
-
wait()
tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ). -
notify()
wakes up the first thread that calledwait()
on the same object.
2. notify() and wait() - example 1
public class ThreadA { public static void main(String[] args){ ThreadB b = new ThreadB(); b.start(); synchronized(b){ try{ System.out.println("Waiting for b to complete..."); b.wait(); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println("Total is: " + b.total); } } } class ThreadB extends Thread{ int total; @Override public void run(){ synchronized(this){ for(int i=0; i<100 ; i++){ total += i; } notify(); } } } |
In the example above, an object, b, is synchronized. b completes the calculation before Main thread outputs its total value.
Output:
Waiting for b to complete... Total is: 4950
If b is not synchonized like the code below:
public class ThreadA { public static void main(String[] args) { ThreadB b = new ThreadB(); b.start(); System.out.println("Total is: " + b.total); } } class ThreadB extends Thread { int total; @Override public void run() { for (int i = 0; i < 100; i++) { total += i; } } } |
The result would be 0, 10, etc. Because sum is not finished before it is used.
3. notify() and wait() - example 2
The second example is more complex, see the comments.
import java.util.Vector; class Producer extends Thread { static final int MAXQUEUE = 5; private Vector messages = new Vector(); @Override public void run() { try { while (true) { putMessage(); //sleep(5000); } } catch (InterruptedException e) { } } private synchronized void putMessage() throws InterruptedException { while (messages.size() == MAXQUEUE) { wait(); } messages.addElement(new java.util.Date().toString()); System.out.println("put message"); notify(); //Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. } // Called by Consumer public synchronized String getMessage() throws InterruptedException { notify(); while (messages.size() == 0) { wait();//By executing wait() from a synchronized block, a thread gives up its hold on the lock and goes to sleep. } String message = (String) messages.firstElement(); messages.removeElement(message); return message; } } class Consumer extends Thread { Producer producer; Consumer(Producer p) { producer = p; } @Override public void run() { try { while (true) { String message = producer.getMessage(); System.out.println("Got message: " + message); //sleep(200); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String args[]) { Producer producer = new Producer(); producer.start(); new Consumer(producer).start(); } } |
A possible output sequence:
Got message: Fri Dec 02 21:37:21 EST 2011 put message put message put message put message put message Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011 put message put message put message put message put message Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011 Got message: Fri Dec 02 21:37:21 EST 2011
<pre><code> String foo = "bar"; </code></pre>
Pingback: Java 2 OCPJP – Threads | TinoChan()
Pingback: efttappingtechniques.com()
Pingback: 监听器-java同步的基本思想 | 并发编程网 - ifeve.com()
Pingback: 免费Simple Java (非常简单的英文) - IT新闻()
Pingback: 免费Simple Java (非常简单的英文) | | Evolution Unit 进化Evolution Unit 进化()