2020.1.30 LeetCode 從零單刷個人筆記整理(持續更新)
github:https://github.com/ChopinXBP/LeetCode-Babel
傳送門:H2O 生成
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 releaseHydrogen and releaseOxygen methods respectively, 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.
In other words:
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.
We don’t have to worry about matching the threads up explicitly; that is, the threads do not necessarily know which other threads they are paired up with. The key is just that threads pass the barrier in complete sets; thus, if we examine the sequence of threads that bond and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.
Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.
現在有兩種線程,氫 oxygen 和氧 hydrogen,你的目标是組織這兩種線程來産生水分子。
存在一個屏障(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。
package JUC;
import java.util.concurrent.Semaphore;
/**
*
* 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 releaseHydrogen
* and releaseOxygen methods respectively, 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.
* In other words:
* 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.
* We don’t have to worry about matching the threads up explicitly; that is, the threads do not necessarily know which other threads they are paired up with.
* The key is just that threads pass the barrier in complete sets; thus, if we examine the sequence of threads that bond and divide them into groups of three,
* each group should contain one oxygen and two hydrogen threads.
* Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.
* 現在有兩種線程,氫 oxygen 和氧 hydrogen,你的目标是組織這兩種線程來産生水分子。
* 存在一個屏障(barrier)使得每個線程必須等候直到一個完整水分子能夠被産生出來。
* 氫和氧線程會被分别給予 releaseHydrogen 和 releaseOxygen 方法來允許它們突破屏障。
* 這些線程應該三三成組突破屏障并能立即組合産生一個水分子。
* 你必須保證産生一個水分子所需線程的結合必須發生在下一個水分子産生之前。
* 換句話說:
* 如果一個氧線程到達屏障時沒有氫線程到達,它必須等候直到兩個氫線程到達。
* 如果一個氫線程到達屏障時沒有其它線程到達,它必須等候直到一個氧線程和另一個氫線程到達。
* 書寫滿足這些限制條件的氫、氧線程同步代碼。
*
*/
public class BuildingH2O {
}
//Semaphore
class H2O {
private Semaphore semaphoreH;
private Semaphore semaphoreO;
public H2O() {
semaphoreH = new Semaphore(2);
semaphoreO = new Semaphore(0);
}
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
try{
semaphoreH.acquire(1);
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
releaseHydrogen.run();
}catch (Exception e){
e.printStackTrace();
}finally {
semaphoreO.release(1);
}
}
public void oxygen(Runnable releaseOxygen) throws InterruptedException {
try{
semaphoreO.acquire(2);
// releaseOxygen.run() outputs "O". Do not change or remove this line.
releaseOxygen.run();
}catch (Exception e){
e.printStackTrace();
}finally {
semaphoreH.release(2);
}
}
}
#Coding一小時,Copying一秒鐘。留個言點個贊呗,謝謝你#