# leetcode.1114-按序打印-多线程案例

### 题目

1114. 按序打印 - 力扣（Leetcode）

public class Foo {
public void first() { print("first"); }
public void second() { print("second"); }
public void third() { print("third"); }
}

• 线程 A 将会调用 first() 方法
• 线程 B 将会调用 second() 方法
• 线程 C 将会调用 third() 方法

• 尽管输入中的数字似乎暗示了顺序，但是我们并不保证线程在操作系统中的调度顺序。
• 你看到的输入格式主要是为了确保测试的全面性。

### 解法一

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();
}
}



### 方法二

CountDownLatch(int count); //构造方法，创建一个值为count 的计数器。

//阻塞当前线程，将当前线程加入阻塞队列。
//当计数器的值变为0时，在CountDownLatch上await()的线程就会被唤醒
await();

await(long timeout, TimeUnit unit);//在timeout的时间之内阻塞当前线程,时间一过则当前线程可以执行，

countDown();//对计数器进行递减1操作，当计数器递减至0时，当前线程会去唤醒阻塞队列里的所有线程。

 class Foo {

private final CountDownLatch firstDone;
private final CountDownLatch secondDone;

public Foo() {
firstDone = new CountDownLatch(1);
secondDone = new CountDownLatch(1);
}

public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
firstDone.countDown();
}

public void second(Runnable printSecond) throws InterruptedException {
firstDone.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
secondDone.countDown();
}

public void third(Runnable printThird) throws InterruptedException {
secondDone.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}

### 方法三

class Foo {

private Semaphore two = new Semaphore(0);
private Semaphore three = 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();
two.release();
}

public void second(Runnable printSecond) throws InterruptedException {
two.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
three.release();
}

public void third(Runnable printThird) throws InterruptedException {
three.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}


### 方法四

public class Foo {
private  int flag = 1;
private final Object object = new Object();

public Foo() {

}

public void first(Runnable printFirst) throws InterruptedException {
synchronized (object) {
while (flag != 1) object.wait();
printFirst.run();
flag = 2;
object.notifyAll();
}
}

public void second(Runnable printSecond) throws InterruptedException {
synchronized (object) {
while (flag != 2) object.wait();
printSecond.run();
flag = 3;
object.notifyAll();
}
}

public void third(Runnable printThird) throws InterruptedException {
synchronized (object) {
while (flag != 3) object.wait();
printThird.run();
}

}
}

• 0
点赞
• 1
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
01-12 195
04-29 74
08-23 273

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。