CountDownLatch CyclicBarrier 和Semaphore

6 篇文章 0 订阅

1.CountDownLatch  

public static void main(String args[]) {
		final CountDownLatch start=new CountDownLatch(1);
		final CountDownLatch end=new CountDownLatch(10);
		
		for(int i=0;i<10;i++) {
			int index=i;
			Thread t=new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						start.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(index+"...");
					end.countDown();
				}
				
			});
			t.start();
		}
		
		System.out.println("start ... ");
		start.countDown();
		try {
			end.await();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("over ... ");
	}

执行结果

start ... 
0...
5...
4...
3...
1...
2...
6...
8...
9...
7...
over ... 

2.CyclicBarrier 

static CyclicBarrier cb=new CyclicBarrier(3);
	
	public static void main(String args[]) {
		
		for(int i=0;i<3;i++) {
			
			final int a=i;
			Thread t=new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					System.out.println(a+"reached ...");
					
					try {
						cb.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					System.out.println(a+"over ...");
				}
				
			});
			t.start();
		}
		
	}

执行结果

0reached ...
1reached ...
2reached ...
2over ...
0over ...
1over ...

3.Semaphore

private static final Semaphore semaphore=new Semaphore(3);
	
	public static void main(String args[]) {
		for(int i=0;i<10;i++) {
			final int a=i;
			Thread t=new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					System.out.println(a+"开始排队 ...");
					try {
						semaphore.acquire();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					System.out.println(a+"得到信号 ...");
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					System.out.println(a+"释放信号 ...");
					
					semaphore.release();
					
					System.out.println("###################当前可用信号"+semaphore.availablePermits());
				}
				
			}) ;
			
			t.start();
		}
	}

执行结果

1开始排队 ...
0开始排队 ...
2开始排队 ...
3开始排队 ...
0得到信号 ...
1得到信号 ...
2得到信号 ...
5开始排队 ...
7开始排队 ...
6开始排队 ...
9开始排队 ...
8开始排队 ...
4开始排队 ...
1释放信号 ...
0释放信号 ...
###################当前可用信号1
5得到信号 ...
2释放信号 ...
###################当前可用信号1
6得到信号 ...
###################当前可用信号1
3得到信号 ...
5释放信号 ...
3释放信号 ...
7得到信号 ...
9得到信号 ...
6释放信号 ...
###################当前可用信号0
8得到信号 ...
###################当前可用信号1
###################当前可用信号1
8释放信号 ...
###################当前可用信号1
7释放信号 ...
###################当前可用信号1
9释放信号 ...
4得到信号 ...
###################当前可用信号2
4释放信号 ...
###################当前可用信号3

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值