我是靠谱客的博主 单身茉莉,最近开发中收集的这篇文章主要介绍畅游多线程之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();
}
}
}

最后

以上就是单身茉莉为你收集整理的畅游多线程之H2O 生成的全部内容,希望文章能够帮你解决畅游多线程之H2O 生成所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部