forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun2_notify.java
More file actions
40 lines (34 loc) · 980 Bytes
/
Copy pathRun2_notify.java
File metadata and controls
40 lines (34 loc) · 980 Bytes
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
32
33
34
35
36
37
38
39
40
package com.brianway.learning.java.multithread.communication.example2;
/**
* Created by Brian on 2016/4/13.
*/
/**
* P139
* notify实现通知
* notify调用后,并不会立即释放对象锁,而是退出synchronized代码块后
*
* 当在一个Synchronized块中调用Sleep()方法是,线程虽然休眠了,但是对象的机锁并木有被释放,其他线程无法访问这个对象
*
* @see Thread2 的run方法
*/
public class Run2_notify {
public static void main(String[] args) {
try {
Object lock = new Object();
Thread1 t1 = new Thread1(lock);
t1.start();
Thread.sleep(3000);
Thread2 t2 = new Thread2(lock);
t2.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
输出:
开始 wait time=1460554213416
开始 notify time=1460554216418
结束 notify time=1460554218418
结束 wait time=1460554218418
*/