本文转载至:http://lavasoft.blog.51cto.com/62575/99157
线程交互是比较复杂的问题,SCJP要求不很基础:给定一个场景,编写代码来恰当使用等待、通知和通知所有线程。
一、线程交互的基础知识
SCJP所要求的线程交互知识点需要从
java.lang.Object的类的三个方法来学习:
void notify()
唤醒在此对象监视器上等待的单个线程。
void notifyAll()
唤醒在此对象监视器上等待的所有线程。
void wait()
导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
当然,wait()还有另外两个重载方法:
void wait(long timeout)
导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量。
void wait(long timeout, int nanos)
导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量。
以上这些方法是帮助线程传递线程关心的时间状态。
关于等待/通知,要记住的关键点是:
必须从同步环境内调用wait()、notify()、notifyAll()方法。线程不能调用对象上等待或通知的方法,除非它拥有那个对象的锁。
wait()、notify()、notifyAll()都是Object的实例方法。与每个对象具有锁一样,每个对象可以有一个线程列表,他们等待来自该信号(通知)。线程通过执行对象上的wait()方法获得这个等待列表。从那时候起,它不再执行任何其他指令,直到调用对象的notify()方法为止。如果多个线程在同一个对象上等待,则将只选择一个线程(不保证以何种顺序)继续执行。如果没有线程等待,则不采取任何特殊操作。
下面看个例子就明白了:
package com.mbl.main;
import com.mbl.MyThread;
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread thread = new MyThread();
//启动计算线程
thread.start();
//线程A拥有b对象上的锁。线程为了调用wait()或notify()方法,该线程必须是那个对象锁的拥有者
synchronized(thread){
try {
System.out.println("等待对象b完成计算。。。");
//当前线程A等待
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("b对象计算的总和是:" + thread.total);
}
}
}
package com.mbl;
public class MyThread extends Thread {
public int total;
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (this) {
for (int i = 0; i < 101; i++) {
total += i;
}
//(完成计算了)唤醒在此对象监视器上等待的单个线程,在本例中线程A被唤醒
notify();
}
}
}
运行截图:
千万注意:
当在对象上调用wait()方法时,执行该代码的线程立即放弃它在对象上的锁。然而调用notify()时,并不意味着这时线程会放弃其锁。如果线程荣然在完成同步代码,则线程在移出之前不会放弃锁。因此,只要调用notify()并不意味着这时该锁变得可用。==(对象调用wait方法,立即放弃对象锁,对象调用notify方法,在同步代码没有执行完毕,不会放弃锁)。
二、多个线程在等待一个对象锁时候使用notifyAll()
在多数情况下,最好通知等待某个对象的所有线程。如果这样做,可以在对象上使用notifyAll()让所有在此对象上等待的线程冲出等待区,返回到可运行状态。
package com.mbl.main;
import com.mbl.message.Message;
import com.mbl.message.Notifier;
import com.mbl.message.Waiter;
public class RunnableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message msg = new Message("process it");
Waiter waiter = new Waiter(msg);
new Thread(waiter,"waiter").start();
Waiter waiter1 = new Waiter(msg);
new Thread(waiter1, "waiter1").start();
Notifier notifier = new Notifier(msg);
new Thread(notifier, "notifier").start();
}
}
package com.mbl.message;
import java.io.Serializable;
public class Message implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
public Message(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.mbl.message;
public class Notifier implements Runnable {
private Message msg;
public Notifier(Message msg) {
this.msg = msg;
}
@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name + " started");
try {
Thread.sleep(1000);
synchronized (msg) {
msg.setMessage(name + " Notifier work done");
//msg.notify();
msg.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.mbl.message;
public class Waiter implements Runnable {
private Message msg;
public Waiter(Message m) {
this.msg = m;
}
@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
synchronized (msg) {
try {
System.out.println(name + " waiting to get notified at time:" + System.currentTimeMillis());
msg.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " waiter thread got notified at time:" + System.currentTimeMillis());
// process the message now
System.out.println(name + " processed: " + msg.getMessage());
}
}
}
当我们调用以上的代码时可以看到以下的输出,但并没有结束(完成),因为有两个线程等待同一个Message对象,但notify()方法只能唤醒一个线程,另一个线程仍然在等待被唤醒。
notify()
waiter waiting to get notified at time:1492417006932
waiter1 waiting to get notified at time:1492417006933
notifier started
waiter waiter thread got notified at time:1492417007934
waiter processed: notifier Notifier work done
如果我们注释掉Notifier类中的notify() 方法的调用,并打开notifyAll() 方法的调用,将会有以下的输出信息。
notifyAll()
waiter waiting to get notified at time:1492417072173
waiter1 waiting to get notified at time:1492417072173
notifier started
waiter1 waiter thread got notified at time:1492417073175
waiter1 processed: notifier Notifier work done
waiter waiter thread got notified at time:1492417073175
waiter processed: notifier Notifier work done
一旦notifyAll()方法唤醒所有的Waiter线程,程序将会执行完成并退出。