-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDealLock.java
51 lines (44 loc) · 1.43 KB
/
DealLock.java
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
41
42
43
44
45
46
47
48
49
50
51
/**
* @Project:
* @Author: leegoo
* @Date: 2019年07月01日
*/
package cn.withme.thread;
/**
* ClassName: DealLock
*
* @author leegoo
* @Description:
* @date 2019年07月01日
*/
public class DealLock {
public static void main(String[] args) {
InnerClass l = new InnerClass();
new Thread(()->{
while (true) l.work1();
}).start();
new Thread(()->{
while (true) l.work2();
}).start();
}
public static class InnerClass {
static Object objectlock1 = new Object();
static Object objectlock2 = new Object();
public void work1 () {
synchronized (objectlock1) {
System.out.println("work1线程:"+Thread.currentThread()+"已经锁住了objectlock1,准备获取objectlock2锁");
synchronized (objectlock2) {
System.out.println("work1线程:"+Thread.currentThread()+"已经锁住了objectlock2,当前任务运行完毕");
}
}
}
public void work2 () {
synchronized (objectlock2) {
System.out.println("work2线程:"+Thread.currentThread()+"已经锁住了objectlock2,准备获取objectlock1锁");
synchronized (objectlock1) {
System.out.println("work2线程:"+Thread.currentThread()+"已经锁住了objectlock1,当前任务运行完毕");
}
}
}
}
}