《java并发编程实战》第五章闭锁和栅栏

先说两点都知道的: 
1.CountDownLatch减计数,CyclicBarrier加计数。 

2.CountDownLatch是一次性的,CyclicBarrier可以重用。 

      闭锁是一种同步工具类,可以延迟线程的进度直到其到达终止状态,当闭锁到达结束状态后,将不会再改变状态。闭锁可以用来确保某些活动直到其他活动都完成后才继续执行。

     countDown方法递减计数器,表示有一个时间发生了,而await方法等待计数器到达为零,这表示所有需要等待的时间都已经发生。如果计数器为非零,那么await会一直阻塞直到计数器为零,或者等到中的线程中断,或者等待超时。

import java.util.concurrent.CountDownLatch;

public class Race {
public static void main(String[] args) {
final int num = 5;
final CountDownLatch begin = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(num);

for (int i = 0; i < num; i++) {
new Thread(new AWorker(i, begin, end)).start();
}

// judge prepare...
try {
Thread.sleep((long) (Math.random() * 5000));
} catch (InterruptedException e1) {
e1.printStackTrace();
}


System.out.println("judge say : run !");
begin.countDown();
long startTime = System.currentTimeMillis();


try {
end.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
long endTime = System.currentTimeMillis();
System.out.println("judge say : all arrived !");
System.out.println("spend time: " + (endTime - startTime));
}


}

}

class AWorker implements Runnable {
final CountDownLatch begin;
final CountDownLatch end;
final int id;


public AWorker(final int id, final CountDownLatch begin,
final CountDownLatch end) {
this.id = id;
this.begin = begin;
this.end = end;
}


@Override
public void run() {
try {
System.out.println(this.id + " ready !");
begin.await();
// run...
Thread.sleep((long) (Math.random() * 10000));
} catch (Throwable e) {
e.printStackTrace();
} finally {
System.out.println(this.id + " arrived !");
end.countDown();
}
}


}



CountDownLatch强调的是一个线程(或多个)需要等待另外的n个线程干完某件事情之后才能继续执行。 上述例子,main线程是裁判,5个AWorker是跑步的。运动员先准备,裁判喊跑,运动员才开始跑(这是第一次同步,对应begin)。5个人谁跑到终点了,countdown一下,直到5个人全部到达,裁判喊停(这是第二次同步,对应end),然后算时间。

2. 继续,还是这五个人(这五个人真无聊..),这次没裁判。规定五个人只要都跑到终点了,大家可以喝啤酒。但是,只要有一个人没到终点,就不能喝。 这里也没有要求大家要同时起跑(当然也可以,加latch)。

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Beer {

public static void main(String[] args) {
final int count = 5;
final CyclicBarrier barrier = new CyclicBarrier(count, new Runnable() {
@Override
public void run() {
System.out.println("drink beer!");
}
});

// they do not have to start at the same time...
for (int i = 0; i < count; i++) {
new Thread(new Worker(i, barrier)).start();
}
}


}

class Worker implements Runnable {
final int id;
final CyclicBarrier barrier;

public Worker(final int id, final CyclicBarrier barrier) {
this.id = id;
this.barrier = barrier;
}

@Override
public void run() {
try {
System.out.println(this.id + "starts to run !");
Thread.sleep((long) (Math.random() * 10000));
System.out.println(this.id + "arrived !");
this.barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值