-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadLock.java
More file actions
45 lines (33 loc) · 1.01 KB
/
DeadLock.java
File metadata and controls
45 lines (33 loc) · 1.01 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
package com.test.multithread;
public class DeadLock {
public static void main(String[] args) throws InterruptedException {
DeadLock a = new DeadLock();
Runnable r1 = () -> a.a();
Runnable r2 = () -> a.b();
Thread t1 = new Thread(r1);
t1.start();
Thread t2 = new Thread(r2);
t2.start();
t1.join();
t2.join();
}
private Object key1 = new Object();
private Object key2 = new Object();
public void a() {
synchronized(key1) {
System.out.println("[" + Thread.currentThread().getName() + "] I am in a()");
b();
}
}
private void b() {
synchronized(key2) {
System.out.println("[" + Thread.currentThread().getName() + "] I am in b()");
c();
}
}
private void c() {
synchronized(key1) {
System.out.println("[" + Thread.currentThread().getName() + "] I am in c()");
}
}
}