参考:http://blog.csdn.net/iceman1952/article/details/2159812
notify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| public class Notify {
private static class T extends Thread {
public T(String s) {
super(s);
}
@Override
public void run() {
System.out.println(String.format("%s run.", this.getName()));
synchronized (Notify.class) {
try {
Notify.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(String.format("%s done.", this.getName()));
}
}
public static void main(String[] args) throws InterruptedException {
new T("thread 1").start();
new T("thread 2").start();
Thread.sleep(1000);
System.out.println("notify");
synchronized (Notify.class) {
Notify.class.notify();
//Notify.class.notifyAll(); //alternative
}
}
}
|
Notify版本主线程是不会退出的,因为释放了一个wait,另一个就会等到天荒地老。
NotifyAll主线程会退出。