package com.example.demo; import java.util.concurrent.Semaphore; /** * @ClassName H2O * @Description: 1117. H2O 生成(多线程) * @Author xtanb * @Date 2019/9/23 * @Version V1.0 **/ public class H2O { private Semaphore h = new Semaphore(2); private Semaphore o = new Semaphore(0); public H2O() { } public void hydrogen(Runnable releaseHydrogen) throws InterruptedException { h.acquire(); releaseHydrogen.run(); o.release(); } public void oxygen(Runnable releaseOxygen) throws InterruptedException { o.acquire(2); releaseOxygen.run(); h.release(2); } }