[Java]LeetCode1117. H2O 生成 | Building H2O

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There are two kinds of threads, oxygen and hydrogen. Your goal is to group these threads to form water molecules. There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given a releaseHydrogen and releaseOxygen method respectfully, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must be able to immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.java

In other words:git

If an oxygen thread arrives at the barrier when no hydrogen threads are present, it has to wait for two hydrogen threads.
If a hydrogen thread arrives at the barrier when no other threads are present, it has to wait for an oxygen thread and another hydrogen thread.
Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.github

 

Example 1:微信

Input: "HOH"
Output: "HHO"
Explanation: "HOH" and "OHH" are also valid answers.
Example 2:ui

Input: "OOHHHH"
Output: "HHOHHO"
Explanation: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.
 this

Constraints:spa

Total length of input string will be 3n, where 1 ≤ n ≤ 50.
Total number of H will be 2n in the input string.
Total number of O will be n in the input string.線程


如今有兩種線程,氫 oxygen 和氧 hydrogen,你的目標是組織這兩種線程來產生水分子。code

存在一個屏障(barrier)使得每一個線程必須等候直到一個完整水分子可以被產生出來。

氫和氧線程會被分別給予 releaseHydrogen 和 releaseOxygen 方法來容許它們突破屏障。

這些線程應該三三成組突破屏障並能當即組合產生一個水分子。

你必須保證產生一個水分子所需線程的結合必須發生在下一個水分子產生以前。

換句話說:

若是一個氧線程到達屏障時沒有氫線程到達,它必須等候直到兩個氫線程到達。
若是一個氫線程到達屏障時沒有其它線程到達,它必須等候直到一個氧線程和另外一個氫線程到達。
書寫知足這些限制條件的氫、氧線程同步代碼。

 

示例 1:

輸入: "HOH"
輸出: "HHO"
解釋: "HOH" 和 "OHH" 依然都是有效解。
示例 2:

輸入: "OOHHHH"
輸出: "HHOHHO"
解釋: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" 和 "OHHOHH" 依然都是有效解。
 

限制條件:

輸入字符串的總長將會是 3n, 1 ≤ n ≤ 50;
輸入字符串中的 「H」 總數將會是 2n;
輸入字符串中的 「O」 總數將會是 n。


22ms

 1 import java.util.concurrent.Semaphore;
 2 
 3 class H2O {
 4 
 5   private Semaphore s1,s2,s3,s4;
 6     
 7     public H2O() {
 8         s1 = new Semaphore(2); // H線程信號量
 9         s2 = new Semaphore(1); // O線程信號量
10         
11         s3 = new Semaphore(0); // 反應條件信號量
12         s4 = new Semaphore(0); // 反應條件信號量
13     }
14 
15     public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
16         s1.acquire(); // 保證只有2個H線程進入執行
17         s3.release(); // 釋放H原子到達信號
18         s4.acquire(); // 等待O原子到達
19         releaseHydrogen.run();
20         s1.release(); // 至關於喚醒1個H線程
21     }
22 
23     public void oxygen(Runnable releaseOxygen) throws InterruptedException {
24         s2.acquire(); // 保證只有1個O線程進入執行
25         s4.release(2); // 釋放O原子到達信號,由於有2個H線程等待因此釋放2個
26         s3.acquire(2); // 等待H原子到達,2個緣由同上
27         releaseOxygen.run();
28         s2.release(); // 至關於喚醒1個O線程
29     }
30 }

23ms

 1 import java.util.concurrent.CountDownLatch;
 2 import java.util.concurrent.Semaphore;
 3 
 4 class H2O {
 5     
 6     private Semaphore semaph1,semaph2,semaph3,semaph4;
 7 
 8     public H2O() {
 9         //用於保證輸出兩個H和一個O
10         semaph1 = new Semaphore(2); // H線程信號量
11         semaph2 = new Semaphore(1);  // O線程信號量
12         
13         //用於確認一次完整的打印是否已經完成
14         semaph3 = new Semaphore(0);
15         semaph4 = new Semaphore(0);
16     }
17 
18     public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
19         semaph1.acquire();  //等待H原子釋放
20         semaph3.release();  // H原子釋放完畢信號
21         semaph4.acquire();  // 等待O原子釋放H信號
22         // releaseHydrogen.run() outputs "H". Do not change or remove this line.
23         releaseHydrogen.run();
24         semaph1.release();
25         //semaph3.release();
26         
27     }
28 
29     public void oxygen(Runnable releaseOxygen) throws InterruptedException {
30         semaph2.acquire();
31         semaph3.acquire(2); //等待H原子釋放完畢信號
32         semaph4.release(2); //釋放兩個H原子
33         // releaseOxygen.run() outputs "O". Do not change or remove this line.
34         releaseOxygen.run();
35         semaph2.release();
36     }
37 }

25ms

 1 import java.util.concurrent.locks.Condition;
 2 import java.util.concurrent.locks.ReentrantLock;    
 3 class H2O {
 4         private volatile boolean flag = true;//true表示打印H
 5         private ReentrantLock lock = new ReentrantLock();
 6         private Condition hcondition = lock.newCondition();
 7         private Condition ocondition = lock.newCondition();
 8         private int i = 0;
 9 
10         public H2O() {
11 
12         }
13 
14         public void hydrogen(Runnable releaseHydrogen) throws InterruptedException{
15             try {
16                 lock.lock();
17                 while (!flag) {
18                     hcondition.await();
19                 }
20                 // releaseHydrogen.run() outputs "H". Do not change or remove this line.
21                 releaseHydrogen.run();
22                 i++;
23                 if (i % 2 == 0) {
24                     flag = false;
25                     ocondition.signalAll();
26                 }
27             } finally {
28                 lock.unlock();
29             }
30 
31 
32         }
33 
34         public void oxygen(Runnable releaseOxygen) throws InterruptedException {
35             try {
36                 lock.lock();
37                 while (flag) {
38                     ocondition.await();
39                 }
40                 // releaseOxygen.run() outputs "H". Do not change or remove this line.
41                 releaseOxygen.run();
42                 flag = true;
43                 hcondition.signalAll();
44             } finally {
45                 lock.unlock();
46             }
47         }
48     }
相關文章
相關標籤/搜索