Semaphore
import java.util.concurrent.Semaphore;
class Foo {
Semaphore s12 = new Semaphore(0);
Semaphore s23 = new Semaphore(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
s12.release();
}
public void second(Runnable printSecond) throws InterruptedException {
s12.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
s23.release();
}
public void third(Runnable printThird) throws InterruptedException {
s23.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
blockingqueue
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
class Foo {
BlockingQueue q12 = new LinkedBlockingQueue();
BlockingQueue q23 = new LinkedBlockingQueue();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
q12.add(1);
}
public void second(Runnable printSecond) throws InterruptedException {
q12.take();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
q23.add(1);
}
public void third(Runnable printThird) throws InterruptedException {
q23.take();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
synchronized
class Foo {
Object obj = new Object();
int v = 1;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (obj) {
while (v != 1) {
obj.wait();
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
v = 2;
obj.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (obj) {
while (v != 2) {
obj.wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
v = 3;
obj.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (obj) {
while (v != 3) {
obj.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
v = 1;
obj.notifyAll();
}
}
}
ReentrnatLock
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class Foo {
ReentrantLock lock = new ReentrantLock();
Condition c1 = lock.newCondition();
Condition c2 = lock.newCondition();
Condition c3 = lock.newCondition();
int v = 1;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
lock.lock();
try {
while (v != 1) {
c1.await();
} // printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
v = 2;
c2.signalAll();
} finally {
lock.unlock();
}
}
public void second(Runnable printSecond) throws InterruptedException {
lock.lock();
try {
while (v != 2) {
c2.await();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
v = 3;
c3.signalAll();
} finally {
lock.unlock();
}
}
public void third(Runnable printThird) throws InterruptedException {
lock.lock();
try {
while (v != 3) {
c3.await();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
v = 1;
c1.signalAll();
} finally {
lock.unlock();
}
}
}