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);
    }
}

 

相关文章:

  • 2021-05-05
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-12-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案