zoukankan      html  css  js  c++  java
  • 经典多线程问题(七)-H20的生产问题

    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);
        }
    }
  • 相关阅读:
    Day10
    Day9
    Day8
    Day7
    Day 6
    Day5
    第一周计划
    事件总线模式辨析
    解释器模式辨析
    解释器模式深度探究
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11718578.html
Copyright © 2011-2022 走看看