-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABCABC.java
More file actions
59 lines (49 loc) · 1.82 KB
/
Copy pathABCABC.java
File metadata and controls
59 lines (49 loc) · 1.82 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
51
52
53
54
55
56
57
58
59
package com.algorithm.demo.interview;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ABCABC {
public static void main(String[] args) throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
Condition conditionC = lock.newCondition();
MyRunnable1 runnableA = new MyRunnable1(lock, conditionA, conditionB, 'A');
MyRunnable1 runnableB = new MyRunnable1(lock, conditionB, conditionC, 'B');
MyRunnable1 runnableC = new MyRunnable1(lock, conditionC, conditionA, 'C');
new Thread(runnableA).start();
Thread.sleep(100);
new Thread(runnableB).start();
Thread.sleep(100);
new Thread(runnableC).start();
Thread.sleep(100);
}
static class MyRunnable1 implements Runnable {
private ReentrantLock lock;
private Condition thisCondition;
private Condition nextCondition;
private char cha;
public MyRunnable1(ReentrantLock lock, Condition condition1, Condition condition2, char chr) {
this.lock = lock;
this.thisCondition = condition1;
this.nextCondition = condition2;
this.cha = chr;
}
@Override
public void run() {
lock.lock();
try {
for (int i = 0; i < 3; i++) {
System.out.println(cha);
nextCondition.signal();
if (i < 2) {
thisCondition.await();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}