1,多个线程通信的问题
-
notify()方法是随机唤醒一个线程
-
notifyAll()方法是唤醒所有线程
-
JDK5之前无法唤醒指定的一个线程
-
如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件
public class Demo2_NotifyAll {
/**
* @param args
*/
public static void main(String[] args) {
final Printer2 p = new Printer2();
new Thread() {
public void run() {
while(true) {
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
try {
p.print2();
} catch (Interrupt