[Java]LeetCode1114. 印刷順|Print in Order

11521 ワード

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★微信公衆番号:山青詠芝(shanqingyongzhi)➤ブログ園住所:山青詠芝(https://www.cnblogs.com/strengthen/)GitHubアドレス:https://github.com/strengthen/LeetCode➤原文住所://www.cnblogs.com/strengthen/p/11289226.html➤リンクが山青詠芝のブログ園のアドレスでなければ、作者の文章を這い出す可能性があります.➤原文は修正され更新されました!原文アドレスをクリックして読むことを強くお勧めします!作者を支持します!オリジナルサポート!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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"); }}3つの異なるスレッドは、1つのFooインスタンスを共有します.
スレッドAがone()メソッドスレッドBを呼び出すtwo()メソッドスレッドCがthree()メソッドを呼び出すthree()メソッドを呼び出すtwo()メソッドの後にtwo()メソッドが実行され、three()メソッドの後にthree()メソッドが実行されるように修正プログラムを設計してください.
例1:
入力:[1,2,3]出力:[onetwothree]解釈:3つのスレッドが非同期で起動されます.[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 }

 
転載先:https://www.cnblogs.com/strengthen/p/11289226.html