-
Notifications
You must be signed in to change notification settings - Fork 297
/
DeadLockDemo.java
54 lines (44 loc) · 1.19 KB
/
DeadLockDemo.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
52
53
54
package com.wangpos.datastructure.java;
/**
* Created by qiyue on 2018/6/15.
*/
public class DeadLockDemo {
private Object lock1 = new Object();
private Object lock2 = new Object();
public void startAllThread(){
new Thread_One().start();
new Thread_Two().start();
}
class Thread_One extends Thread{
@Override
public void run() {
super.run();
synchronized (lock1){
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2){
System.out.println("线程 1在执行");
}
}
}
}
class Thread_Two extends Thread{
@Override
public void run() {
super.run();
synchronized (lock2){
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1){
System.out.println("线程 2在执行");
}
}
}
}
}