畅游多线程之H2O 生成

1117. H2O 生成

方法1:Semaphore

  • Semaphore是一个计数信号量。
  • 从概念上将,Semaphore包含一组许可证。
  • 如果有需要的话,每个acquire()方法都会阻塞,直到获取一个可用的许可证。
  • 每个release()方法都会释放持有许可证的线程,并且归还Semaphore一个可用的许可证。
  • 然而,实际上并没有真实的许可证对象供线程使用,Semaphore只是对可用的数量进行管理维护
  • 总结:如果线程要访问一个资源就必须先获得信号量。如果信号量内部计数器大于0,信号量减1,然后允许共享这个资源;否则,如果信号量的计数器等于0,信号量将会把线程置入休眠直至计数器大于0.当信号量使用完时,必须释放
class H2O {


    private Semaphore hSema = new Semaphore(2);
    private Semaphore oSema = new Semaphore(0);


    public H2O() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        hSema.acquire();
        releaseHydrogen.run();
        oSema.release();
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        oSema.acquire(2);
        releaseOxygen.run();
        hSema.release(2);
    }
}

方法2:Semaphore+CyclicBarrier

  • CyclicBarrier会自动充值
class H2O {
    private Semaphore hSema = new Semaphore(2);
    private Semaphore oSema = new Semaphore(1);
    private CyclicBarrier cb = new CyclicBarrier(3);

    public H2O() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        hSema.acquire();
        try {
            cb.await();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        releaseHydrogen.run();
        hSema.release();
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        oSema.acquire();
        try {
            cb.await();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        releaseOxygen.run();
        oSema.release();
    }
}

方法3:ReentrantLock+Condition

使用两个变量进行标记,每次满足条件后,重新开启下一轮

class H2O {

    private int oCnt = 0;
    private int hCnt = 0;
    private ReentrantLock lock = new ReentrantLock();
    private Condition con = lock.newCondition();

    public H2O() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        lock.lock();
        try {
            while (hCnt == 2) {
                con.await();
            }
            hCnt++;
            if (hCnt == 2 && oCnt == 1) {
                hCnt = 0;
                oCnt = 0;
            }
            releaseHydrogen.run();
            con.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        lock.lock();
        try {
            while (oCnt == 1) {
                con.await();
            }
            oCnt++;
            if (hCnt == 2 && oCnt == 1) {
                hCnt = 0;
                oCnt = 0;
            }
            releaseOxygen.run();
            con.signalAll();
        } finally {
            lock.unlock();
        }

    }
}

方法4:synchronized

class H2O {

    private volatile int state = 0;
    private Object obj = new Object();

    public H2O() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        synchronized (obj) {
            while (state == 2) {
                obj.wait();
            }
            state++;
            releaseHydrogen.run();
            obj.notifyAll();
        }

    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        synchronized (obj) {
            while (state != 2) {
                obj.wait();
            }
            state = 0;
            releaseOxygen.run();
            obj.notifyAll();
        }

    }
}

方法5:BlockingQueue

class H2O {

    private int cnt = 0;
    private BlockingQueue<Integer> hQ = new LinkedBlockingDeque<>(2);
    private BlockingQueue<Integer> oQ = new LinkedBlockingDeque<>(1);

    public H2O() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        hQ.put(1);
        releaseHydrogen.run();
        cnt++;
        if (cnt == 3) {
            cnt = 0;
            hQ.clear();
            oQ.clear();
        }
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        oQ.put(1);
        releaseOxygen.run();
        cnt++;
        if (cnt == 3) {
            cnt = 0;
            hQ.clear();
            oQ.clear();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值