class Foo {
public:
Foo() {
}
void first(function<void()> printFirst) {
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
}
void second(function<void()> printSecond) {
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
}
void third(function<void()> printThird) {
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};
class Foo {
private AtomicInteger firstJobDone = new AtomicInteger(0);
private AtomicInteger secondJobDone = new AtomicInteger(0);
public Foo() {}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first".
printFirst.run();
// mark the first job as done, by increasing its count.
firstJobDone.incrementAndGet();
}
public void second(Runnable printSecond) throws InterruptedException {
while (firstJobDone.get() != 1) {
// waiting for the first job to be done.
}
// printSecond.run() outputs "second".
printSecond.run();
// mark the second as done, by increasing its count.
secondJobDone.incrementAndGet();
}
public void third(Runnable printThird) throws InterruptedException {
while (secondJobDone.get() != 1) {
// waiting for the second job to be done.
}
// printThird.run() outputs "third".
printThird.run();
}
}
# 使用线程锁,2把锁
from threading import Lock
class Foo:
def __init__(self):
self.firstJobDone = Lock()
self.secondJobDone = Lock()
self.firstJobDone.acquire()
self.secondJobDone.acquire()
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first".
printFirst()
# Notify the thread that is waiting for the first job to be done.
self.firstJobDone.release()
def second(self, printSecond: 'Callable[[], None]') -> None:
# Wait for the first job to be done
with self.firstJobDone:
# printSecond() outputs "second".
printSecond()
# Notify the thread that is waiting for the second job to be done.
self.secondJobDone.release()
def third(self, printThird: 'Callable[[], None]') -> None:
# Wait for the second job to be done.
with self.secondJobDone:
# printThird() outputs "third".
printThird()
import java.util.concurrent.CountDownLatch;
class Foo {
// 计数器通过使用锁(共享锁、排它锁)实现
private CountDownLatch second = new CountDownLatch(1);
private CountDownLatch third = new CountDownLatch(1);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
second.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
second.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
third.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
third.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
import java.util.concurrent.Semaphore;
class Foo {
// 定义两个信号量, 分别是1->2, 2->3; 通过信号量的 获取-释放 控制流程
public Semaphore seam_first_two = new Semaphore(0);
public Semaphore seam_two_second = 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();
seam_first_two.release();
}
public void second(Runnable printSecond) throws InterruptedException {
seam_first_two.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
seam_two_second.release();
}
public void third(Runnable printThird) throws InterruptedException {
seam_two_second.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}