为有牺牲多壮志,敢教日月换新天。

[Java]LeetCode1114. 按序打印 | Print in Order

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址://www.cnblogs.com/strengthen/p/11289226.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Suppose we have a class:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}
The same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().

Example 1:

Input: [1,2,3]
Output: "firstsecondthird"
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.
Example 2:

Input: [1,3,2]
Output: "firstsecondthird"
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output.

Note:

We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.


我们提供了一个类:

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"。

注意:

尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。

你看到的输入格式主要是为了确保测试的全面性。


10ms

 1 import java.util.concurrent.Semaphore;
 2 
 3 class Foo {
 4     Semaphore A;
 5     Semaphore B;
 6     Semaphore C;
 7     
 8     public Foo() {
 9         A = new Semaphore(1);
10         B = new Semaphore(0);
11         C = new Semaphore(0);
12     }
13 
14     public void first(Runnable printFirst) throws InterruptedException {
15         A.acquire();
16         // printFirst.run() outputs "first". Do not change or remove this line.
17         printFirst.run();
18         B.release();
19     }
20 
21     public void second(Runnable printSecond) throws InterruptedException {
22         B.acquire();
23         // printSecond.run() outputs "second". Do not change or remove this line.
24         printSecond.run();
25         C.release();
26     }
27 
28     public void third(Runnable printThird) throws InterruptedException {
29         C.acquire();
30         // printThird.run() outputs "third". Do not change or remove this line.
31         printThird.run();
32     }
33 }

13ms

 1 class Foo {
 2 
 3    private boolean firstFinished;
 4     private boolean secondFinished;
 5     private Object lock = new Object();
 6 
 7     public Foo() {
 8         
 9     }
10 
11     public void first(Runnable printFirst) throws InterruptedException {
12         
13         synchronized (lock) {
14             // printFirst.run() outputs "first". Do not change or remove this line.
15             printFirst.run();
16             firstFinished = true;
17             lock.notifyAll(); 
18         }
19     }
20 
21     public void second(Runnable printSecond) throws InterruptedException {
22         
23         synchronized (lock) {
24             while (!firstFinished) {
25                 lock.wait();
26             }
27         
28             // printSecond.run() outputs "second". Do not change or remove this line.
29             printSecond.run();
30             secondFinished = true;
31             lock.notifyAll();
32         }
33     }
34 
35     public void third(Runnable printThird) throws InterruptedException {
36         
37         synchronized (lock) {
38            while (!secondFinished) {
39                 lock.wait();
40             }
41 
42             // printThird.run() outputs "third". Do not change or remove this line.
43             printThird.run();
44         } 
45     }
46 }

 

posted @ 2019-08-02 16:15  为敢技术  阅读(1116)  评论(0编辑  收藏  举报