-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadLock.java
More file actions
49 lines (43 loc) · 1.58 KB
/
Copy pathDeadLock.java
File metadata and controls
49 lines (43 loc) · 1.58 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
41
42
43
44
45
46
47
48
49
package sync;
import java.util.concurrent.TimeUnit;
/**
* @author : CodeWater
* @create :2022-06-02-23:08
* @Function Description :演示死锁
* A线程已经持有a,又想获取b
* B线程已经持有b,又想获取a
*/
public class DeadLock {
//创建两个对象
static Object a = new Object();
static Object b = new Object();
public static void main(String[] args) {
new Thread( () -> {
synchronized( a ){
System.out.println(Thread.currentThread().getName()+" 持有锁a,试图获取锁b");
try{
//为了显示出效果,睡眠1秒。 因为线程创建的顺序不定,所以这里为了确定顺序
TimeUnit.SECONDS.sleep( 1 );
}catch( InterruptedException e ){
e.printStackTrace();
}
synchronized( b ){
System.out.println(Thread.currentThread().getName()+" 获取锁b");
}
}
} , "A" ).start();
new Thread( () -> {
synchronized( b ){
System.out.println(Thread.currentThread().getName()+" 持有锁b,试图获取锁a");
try{
TimeUnit.SECONDS.sleep( 1 );
}catch (InterruptedException e ){
e.printStackTrace();
}
synchronized( a ){
System.out.println(Thread.currentThread().getName()+" 获取锁a");
}
}
} , "B" ).start();
}
}