-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplicitLockInfo.java
More file actions
40 lines (38 loc) · 1.06 KB
/
Copy pathExplicitLockInfo.java
File metadata and controls
40 lines (38 loc) · 1.06 KB
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
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 演示线程转储显示锁信息的示例程序
*
* @author Kyle
* @create 2018/3/17 16:12
*/
public class ExplicitLockInfo {
private static final Lock lock = new ReentrantLock();
private static int sharedData = 0;
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
try {
Thread.sleep(2200000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sharedData = 1;
} finally {
lock.unlock();
}
}
});
t.start();
Thread.sleep(100);
lock.lock();
try {
System.out.println("sharedData:" + sharedData);
} finally {
lock.unlock();
}
}
}