AtomicInteger count = new AtomicInteger(); publicFoo(){ count.set(0); }
publicvoidfirst(Runnable printFirst)throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. while(!count.compareAndSet(0, 1)){} printFirst.run(); }
publicvoidsecond(Runnable printSecond)throws InterruptedException { // printSecond.run() outputs "second". Do not change or remove this line. while(!count.compareAndSet(1,2)){} printSecond.run(); }
publicvoidthird(Runnable printThird)throws InterruptedException { // printThird.run() outputs "third". Do not change or remove this line. while(!count.compareAndSet(2,3)){} printThird.run(); } }
publicFoo(){ this.secondLatch = new CountDownLatch(1); this.thirdLatch = new CountDownLatch(1); }
publicvoidfirst(Runnable printFirst)throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run(); this.secondLatch.countDown(); }
publicvoidsecond(Runnable printSecond)throws InterruptedException { // printSecond.run() outputs "second". Do not change or remove this line. this.secondLatch.await(); printSecond.run(); this.thirdLatch.countDown(); }
publicvoidthird(Runnable printThird)throws InterruptedException { // printThird.run() outputs "third". Do not change or remove this line. this.thirdLatch.await(); printThird.run(); } }
import java.util.concurrent.*; class Foo { Semaphore run2, run3; public Foo() { run2 = new Semaphore(0); run3 = new Semaphore(0); }
public void first(Runnable printFirst) throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run(); run2.release(); }
public void second(Runnable printSecond) throws InterruptedException { // printSecond.run() outputs "second". Do not change or remove this line. run2.acquire(); printSecond.run(); run3.release(); }
public void third(Runnable printThird) throws InterruptedException { // printThird.run() outputs "third". Do not change or remove this line. run3.acquire(); printThird.run(); } }