本文共 4139 字,大约阅读时间需要 13 分钟。
/** * 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); }
/** * 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; }
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
转载地址:http://qghif.baihongyu.com/