public class PrintABC_Condition {
private Lock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
private Condition conditionC = lock.newCondition();
private char currThreadName = 'A';
public void printABC(){
System.out.println();
Thread threadA = new Thread(new ThreadA());
Thread threadB = new Thread(new ThreadB());
Thread threadC = new Thread(new ThreadC());
threadA.start();
threadB.start();
threadC.start();
}
private class ThreadA implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i) {
lock.lock();
try {
while (currThreadName != 'A') {
try {
conditionA.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("A");
currThreadName = 'B';