CyclicBarrier,根据字面意思理解:循环屏障。屏障的意思:CyclicBarrier可以让一组线程到达某个屏障点时被阻塞,直到最后一个线程到达屏障点时,屏障才会放开,所有被屏障阻塞的线程才开始执行任务;循环:当所有线程被释放后,这个CyclicBarrier可以被重用。
有两个很常用的构造方法:
第一个:
/**
* Creates a new {@code CyclicBarrier} that will trip when the
* given number of parties (threads) are waiting upon it, and
* does not perform a predefined action when the barrier is tripped.
*
* @param parties the number of threads that must invoke {@link #await}
* before the barrier is tripped
* @throws IllegalArgumentException if {@code parties} is less than 1
*/
public CyclicBarrier(int parties) {
this(parties, null);
}
这个会创建一个同步屏障,参数表示屏障拦截的线程数量,这些线程需要调用await()方法告诉CyclicBarrier自己到达了屏障点,然后阻塞住,等待其他线程,直到最后一个线程到达屏障点。
第二个:
/**
* Creates a new {@code CyclicBarrier} that will trip when the
* given number of parties (threads) are waiting upon it, and which
* will execute the given barrier action when the barrier is tripped,
* performed by the last thread entering the barrier.
*
* @param parties the number of threads that must invoke {@link #await}
* before the barrier is tripped
* @param barrierAction the command to execute when the barrier is
* tripped, or {@code null} if there is no action
* @throws IllegalArgumentException if {@code parties} is less than 1
*/
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}
这个和上面的方法的区别是,当所有线程释放后,会随机选择一个线程来执行barrierAction。
简单示例:
package com.java4all.mypoint;
import java.time.LocalTime;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
* Author: yunqing
* Date: 2018/7/23
* Description:
*/
public class MyRunnable implements Runnable{
public CyclicBarrier cyclicBarrier;
@Override
public void run() {
try {
Thread.sleep(20000);
System.out.println("子线程正在执行任务,当前线程为:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString());
cyclicBarrier.await();
}catch (BrokenBarrierException bbnex){
bbnex.printStackTrace();
}catch (InterruptedException inex){
inex.printStackTrace();
}
}
public CyclicBarrier getCyclicBarrier() {
return cyclicBarrier;
}
public void setCyclicBarrier(CyclicBarrier cyclicBarrier) {
this.cyclicBarrier = cyclicBarrier;
}
}
package com.java4all.mypoint;
import java.time.LocalTime;
import java.util.concurrent.CyclicBarrier;
/**
* Author: yunqing
* Date: 2018/7/18
* Description:线程测试
* 测试点:一组线程执行完后再执行某任务
*/
public class ThreadTest {
private static CyclicBarrier cyclicBarrier;
public static void main(String[] args)throws Exception{
System.out.println("主线程正在执行前:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString());
cyclicBarrier = new CyclicBarrier(10, new Runnable() {
@Override
public void run() {
System.out.println("------子线程都执行完了吧!执行任务A!当前线程为:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString());
}
});
for(int i = 1;i <= 10;i++){
MyRunnable myRunnable = new MyRunnable();
myRunnable.setCyclicBarrier(cyclicBarrier);
new Thread(myRunnable).start();
}
System.out.println("主线程正在执行后:"+Thread.currentThread().getName()+" 时间:"+ LocalTime.now().toString());
}
}
执行结果为:
主线程正在执行前:main 时间:17:30:47.103
主线程正在执行后:main 时间:17:30:47.105
子线程正在执行任务,当前线程为:Thread-5 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-4 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-6 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-0 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-1 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-2 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-3 时间:17:31:07.105
子线程正在执行任务,当前线程为:Thread-9 时间:17:31:07.106
子线程正在执行任务,当前线程为:Thread-7 时间:17:31:07.106
子线程正在执行任务,当前线程为:Thread-8 时间:17:31:07.106
------子线程都执行完了吧!执行任务A!当前线程为:Thread-4 时间:17:31:07.107