我们提供了一个类:
public class Foo {
public void one() { print("one"); }
public void two() { print("two"); }
public void three() { print("three"); }
}
三个不同的线程将会共用一个 Foo 实例。
线程 A 将会调用 one() 方法
线程 B 将会调用 two() 方法
线程 C 将会调用 three() 方法
请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。
示例 1:
输入: [1,2,3]
输出: "onetwothree"
解释:
有三个线程会被异步启动。
输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
正确的输出是 "onetwothree"。
示例 2:
输入: [1,3,2]
输出: "onetwothree"
解释:
输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
正确的输出是 "onetwothree"。
方案一:
这是一个典型的执行屏障的问题,可以通过构造屏障来实现。
如下图,我们需要构造 22 道屏障,second 线程等待 first 屏障,third 线程等待 second 屏障。:
first 线程会释放 first 屏障,而 second 线程会释放 second 屏障。
Java 中,我们使用线程等待的方式实现执行屏障,使用释放线程等待的方式实现屏障消除。具体代码如下:
class Foo {
private boolean firstFinished;
private boolean secondFinished;
private Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (lock) {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
firstFinished = true;
lock.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (lock) {
while (!firstFinished) {
lock.wait(); //wait()方法将处于等待状态,并且释放资源
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
secondFinished = true;
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (lock) {
while (!secondFinished) {
lock.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
}
方案二:
class Foo {
public Foo() {
}
volatile int count=1;
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
count++;
}
public void second(Runnable printSecond) throws InterruptedException {
while (count!=2);
printSecond.run();
count++;
}
public void third(Runnable printThird) throws InterruptedException {
while (count!=3);
printThird.run();
}
}
方案三:
countDownLatch这个类使一个线程等待其他线程各自执行完毕后再执行。是通过一个计数器来实现的,计数器的初始值是线程的数量。每当一个线程执行完毕后,计数器的值就-1,当计数器的值为0时,表示所有线程都执行完毕,然后在闭锁上等待的线程就可以恢复工作了。
1:首先考虑使用一个线程安全的线程标记,选择了countDownLatch计数器。
2:根据题目描述就是在线程1,2,3不论谁先执行,都要使执行结果为first - second - third
3:分析countDownLatch计数器,他的作用就是初始化一个计数器数值指定,只有在该计数器的值为0时才继续执行。
1、我们先创建两个计数器 c2 是second的,c3是third 初始化都为1.
2、我们可以使用它的countDown(解释:使当前计数器减一)方法在first执行后将c2的计数器置为0,在c2执行 后通过.countDown方法将c3的计数器置为0。
3、同时在应对如果 second、third先执行,在他们之中.await(解释:如果当前计数器不为0,那么挂起,等待 计数器为0后继续执行)自己的计数器。
class Foo {
private CountDownLatch c2;
private CountDownLatch c3;
public Foo() {
c2 = new CountDownLatch(1);
c3 = new CountDownLatch(1);
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
c2.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
c2.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
c3.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
c3.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}

