★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公眾號:山青詠芝(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"); }
}
三個不同的線程將會共用一個 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 }