CountDownLatch latch = new CountDownLatch(6):
6:代表初始扣除点,为零则继续执行,可以自行设置
latch.countDown():表示扣除点-1
latch.await():阻塞线程,直至我们的扣除点为零才会唤醒线程,这里使用需要注意
/**
*类说明:演示CountDownLatch,模拟5个初始化的线程,6个扣除点(注:一个线程可以执行多个扣除)
*扣除完毕以后,主线程和业务线程才能继续自己的工作
*/
public class UseCountDownLatch {
private static CountDownLatch latch = new CountDownLatch(6);
/**
* 初始化线程(有4个)
*/
private static class InitThread implements Runnable{
@Override
public void run() {
System.out.println("Thread_"+Thread.currentThread().getId()
+" ready init work......");
latch.countDown();//初始化线程完成工作了,countDown方法只扣减一次;
for(int i =0;i<2;i++) {
System.out.println("Thread_"+Thread.currentThread().getId()
+" ........continue do its work");
}
}
}
/**
* 业务线程
*/
private static class BusiThread implements Runnable{
@Override
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i =0;i<3;i++) {
System.out.println("BusiThread_"+Thread.currentThread().getId()
+" do business-----");
}
}
}
public static void main(String[] args) throws InterruptedException {
//单独的初始化线程,初始化分为2步,需要扣减两次
new Thread(new Runnable() {
@Override
public void run() {
SleepTools.ms(1);
System.out.println("Thread_"+Thread.currentThread().getId()
+" ready init work step 1st......");
latch.countDown();//扣减一次
System.out.println("begin step 2nd.......");
SleepTools.ms(1);
System.out.println("Thread_"+Thread.currentThread().getId()
+" ready init work step 2nd......");
latch.countDown();//扣减一次
}
}).start();
new Thread(new BusiThread()).start();
//另外的4个初始化线程
for(int i=0;i<=3;i++){
Thread thread = new Thread(new InitThread());
thread.start();
}
latch.await();
System.out.println("Main do ites work........");
}
}
可以自行拿代码输出来观察一下
CyclicBarrier barrier = new CyclicBarrier(5,new CollectThread()):
5:代表多少个线程抵达await方法时,执行屏障后线程,可以自行设置
new CollectThread:当线程都抵达await方法时,那么屏障去除,执行此线程,需要实现Runnable接口
/**
*类说明:CyclicBarrier的使用
*/
public class UseCyclicBarrier {
private static CyclicBarrier barrier
= new CyclicBarrier(5,new CollectThread());
private static ConcurrentHashMap<String,Long> resultMap
= new ConcurrentHashMap<>();//存放子线程工作结果的容器
public static void main(String[] args) {
for(int i=0;i<=4;i++){
Thread thread = new Thread(new SubThread());
thread.start();
}
}
//负责屏障开放以后的工作
private static class CollectThread implements Runnable{
@Override
public void run() {
StringBuilder result = new StringBuilder();
for(Map.Entry<String,Long> workResult:resultMap.entrySet()){
result.append("["+workResult.getValue()+"]");
}
System.out.println(" the result = "+ result);
System.out.println("do other business........");
}
}
//工作线程
private static class SubThread implements Runnable{
@Override
public void run() {
long id = Thread.currentThread().getId();//线程本身的处理结果
resultMap.put(Thread.currentThread().getId()+"",id);
Random r = new Random();//随机决定工作线程的是否睡眠,来判断是否唤醒屏障后线程是等待所有工作线程都执行完抵达await后再执行
try {
//这里随机布尔值,ture或false
if(r.nextBoolean()) {
Thread.sleep(2000+id);
System.out.println("Thread_"+id+" ....do something ");
}
System.out.println(id+"....is await");
barrier.await();
Thread.sleep(1000+id);
System.out.println("Thread_"+id+" ....do its business ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}