🌕 🌗 1117. H2O 生成
2022年6月20日
- algorithm
🌕 🌗 1117. H2O 生成
难度: 🌕 🌗
问题描述
解法 - Semaphore
class H2O {
Semaphore so;
Semaphore sh;
public H2O() {
this.so = new Semaphore(0);
this.sh = new Semaphore(2);
}
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
sh.acquire();
releaseHydrogen.run();
so.release();
}
public void oxygen(Runnable releaseOxygen) throws InterruptedException {
so.acquire(2);
releaseOxygen.run();
sh.release(2);
}
}