import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class book {
public static void main(String[] args) throws InterruptedException {
accurateSignal da = new accurateSignal();
new Thread(()->{
for(int i = 0; i < 10; i++){
try {
da.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
for(int i = 0; i < 10; i++){
try {
da.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(()->{
for(int i = 0; i < 10; i++){
try {
da.printC();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
class accurateSignal{
Lock lock = new ReentrantLock();
Condition a1 = lock.newCondition();
Condition b1 = lock.newCondition();
Condition c1 = lock.newCondition();
int num = 1;
public void printA() throws InterruptedException {
lock.lock();
while(num!=1){
a1.await();
}
System.out.println("A");
num = 2;
b1.signal();
lock.unlock();
}
public void printB() throws InterruptedException {
lock.lock();
while(num!=2){
b1.await();
}
System.out.println("B");
num = 3;
c1.signal();
lock.unlock();
}
public void printC() throws InterruptedException {
lock.lock();
while(num!=3){
c1.await();
}
System.out.println("C");
num = 1;
a1.signal();
lock.unlock();
}
}