-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeadReentrantLock.java
More file actions
50 lines (43 loc) · 1.3 KB
/
Copy pathDeadReentrantLock.java
File metadata and controls
50 lines (43 loc) · 1.3 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
50
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DeadReentrantLock{
private static volatile boolean isADone = false;
private static volatile boolean isBDone = false;
public static void main(String[] args){
final Lock lock1 = new ReentrantLock();
final Lock lock2 = new ReentrantLock();
ExecutorService es = Executors.newFixedThreadPool(2);
Runnable r1 = new Runnable(){
public void run(){
lock1.lock();
System.out.println(Thread.currentThread() + " Do something A: " + lock1);
lock2.lock();
System.out.println(Thread.currentThread() + " Do something B: " + lock2);
lock1.unlock(); // for A
lock2.unlock(); // for B
isADone = true;
if(isADone&&isBDone){
es.shutdown();
}
}
};
es.submit(r1);
Runnable r2 = new Runnable(){
public void run(){
lock2.lock();
System.out.println(Thread.currentThread() + " Do something ONE: " + lock2);
lock1.lock();
System.out.println(Thread.currentThread() + " Do something TWO: " + lock1);
lock2.unlock(); // for ONE
lock1.unlock(); // for TWO
isBDone = true;
if(isADone&&isBDone){
es.shutdown();
}
}
};
es.submit(r2);
}
}