Java并发-CountDownLatch、CyclicBarrier和Semaphore

1.Semaphore

Semaphore,中文名信号量,Semaphore可以控制访问该资源的线程数量。
通过构造方法来申明所拥有的资源额个数。

    public Semaphore(int permits) {
        sync = new NonfairSync(permits);
    }
    public Semaphore(int permits, boolean fair) {
        sync = fair ? new FairSync(permits) : new NonfairSync(permits);
    }

通过acquire()获得许可,如果没有就等待,release()释放许可,tryAcquire()来判断是否还有空闲的资源,也可设置等待的时间。

  public void acquire() throws InterruptedException {}
  public void acquire(int permits) throws InterruptedException {}
  public void release(int permits) {}
  public void release() {}
  public boolean tryAcquire() {}
  public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException { };  
  public boolean tryAcquire(int permits) { }
  public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException { };

简单DEMO,共有5台机器可以操作,每个线程表示一个工人。

public class test1 implements Runnable{
	private Machine ma = new Machine();

	public static void main(String[] args) throws InterruptedException {
		Runnable r = new test1();
		for(int i=0;i<8;i++) {
			Thread t =  new Thread(r);
			t.start();
		}
	}
	@Override
	public  void run() {
		System.out.println(Thread.currentThread()+"获得生产任务");
		ma.work(Thread.currentThread());
	}	
}
class Machine{
	//机器的数量
	private int count = 5 ;
	Semaphore se = new Semaphore(count);
	public void work(Thread t) {
		try {
			se.acquire();
			System.out.println(t.getName()+"在进行运行");
			t.sleep(2000);
			System.out.println(t.getName()+"运行完毕");
			se.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

2.CountDownLatch

CountDownLatch,中文倒数闸门,当一个线程需要等待其他线程完成后再继续下一步,有一点join()方法的意思。

  • 通过构造方法来申明需要等待的线程数量。
public CountDownLatch(int count) {}
  • 关键方法
//让线程进入等待,知道count的数值为0时继续进行
public void await() throws InterruptedException {}
//和await一样,只是多了一个最长的等待时间
public boolean await(long timeout, TimeUnit unit){}
//count的数值减1
public void countDown() {}
  • 简单DEMO
public class test1 implements Runnable{
	private static CountDownLatch cdl = new CountDownLatch(3);
	public static void main(String[] args) throws InterruptedException {
		Runnable r = new test1();
		for(int i=0;i<3;i++) {
			Thread t =  new Thread(r);
			t.start();
		}	
		System.out.println("等待其他线程工作完成");
		cdl.await();
		System.out.println("工作已完成,继续下一步");		
	}
	@Override
	public  void run() {
		try {
			System.out.println(Thread.currentThread()+"获得生产任务");
			Thread.currentThread().sleep(1000);
			System.out.println(Thread.currentThread().getName()+"运行完毕");
			cdl.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}	
}

3.CyclicBarrier

CyclicBarrier,字面意思循环屏障,和CountDownLatch有点相似,只不过CountDownLatch是一个线程等待着其他线程完成后再进行执行,CyclicBarrier是用来表明一组线程全部都达到Barrier状态后再一起向下执行,有种好像好兄弟要走一起走的赶脚,且可以多个线程组进行重用。

  • 构造方法
    通过构造方法来申明好兄弟的数量,parties表示数量,Runnable barrierAction 参数为runnable,当所有的线程都到达Barrier状态后,会选择其中一个进行运行该runnable。
public CyclicBarrier(int parties, Runnable barrierAction) {}
public CyclicBarrier(int parties) {}
  • 关键方法
    将线程的状态设置为Barrier,可设置最长的等待时间,若没有,则抛出一个异常继续向下执行。
public int await() throws InterruptedException, BrokenBarrierException {}
public int await(long timeout, TimeUnit unit)throws InterruptedException,BrokenBarrierException,TimeoutException {
  • 简单DEMO
public class test1 implements Runnable{
	private static CyclicBarrier ccb = new CyclicBarrier(3);
	public static void main(String[] args) throws InterruptedException {
		Runnable r = new test1();
		for(int i=0;i<3;i++) {
			Thread t =  new Thread(r);
			t.start();
		}
	}
	@Override
	public  void run() {
		try {
			System.out.println(Thread.currentThread()+"获得生产任务");
			Thread.currentThread().sleep(1000);
			System.out.println(Thread.currentThread().getName()+"生产完毕,在线等待其他线程生产完毕");
			ccb.await();
			System.out.println(Thread.currentThread().getName()+"继续向下生产");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
	}	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值