力扣 |
您所在的位置:网站首页 › 010203040506 › 力扣 |
1114. 按序打印
我们提供了一个类: public class Foo { public void first() { print("first"); } public void second() { print("second"); } public void third() { print("third"); } }三个不同的线程将会共用一个 Foo 实例。 线程 A 将会调用 first() 方法 线程 B 将会调用 second() 方法 线程 C 将会调用 third() 方法请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/print-in-order 示例 1: 输入: [1,2,3] 输出: “firstsecondthird” 解释: 有三个线程会被异步启动。 输入 [1,2,3] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 second() 方法,线程 C 将会调用 third() 方法。 正确的输出是 “firstsecondthird”。 示例 2: 输入: [1,3,2] 输出: “firstsecondthird” 解释: 输入 [1,3,2] 表示线程 A 将会调用 first() 方法,线程 B 将会调用 third() 方法,线程 C 将会调用 second() 方法。 正确的输出是 “firstsecondthird”。 提示: 尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。 你看到的输入格式主要是为了确保测试的全面性。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/print-in-order 官方题解 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". Do not change or remove this line. 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". Do not change or remove this line. 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". Do not change or remove this line. printThird.run(); } }
我们使用一个 Ojbect 对象 lock 实现所有执行屏障的锁对象,两个布尔型对象 firstFinished,secondFinished 保存屏障消除的条件。 作者:pulsaryu 链接:https://leetcode-cn.com/problems/print-in-order/solution/gou-zao-zhi-xing-ping-zhang-shi-xian- 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==false) { lock.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(); //lock.notifyAll();//不用通知其他线程,因为它肯定是最后执行的 } } } 1115. 交替打印FooBar我们提供一个类: class FooBar { public void foo() { for (int i = 0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |