个人学习笔记,如有错误欢迎指正。。
CyclicBarrier 类用于阻塞一个或多个线程,等待其它线程完成任务,直至所有线程都完成各自的任务,才会解除阻塞继续执行。
构建器 CyclicBarrier (int n,Runnable runnable)
await()方法被调用时,计数器N减一,如果计数据N>0,则当前线程阻塞,如果计数器N==0时,则 Runnable 被调用, Runnable 执行完成后,所有因为await()方法阻塞的线程被唤醒。N被再次重置为初始值,这个 CyclicBarrier 可以被再次复用。
示例:计算1+2+3+4+5+6+........+10000=??
由多个线程执行计算,每个线程计算的结果相加就等于最终结果,并每个线程打印出最终结果。
public class TestCyclicBarrier implements Runnable{ CyclicBarrier cyclicBarrier = null; int startValue; int endValue; AtomicLong total ; String threadName ; String resultReceiver; public TestCyclicBarrier(String threadName,CyclicBarrier cyclicBarrier,AtomicLong total,int startValue,int endValue,String resultReceiver){ this.cyclicBarrier = cyclicBarrier; this.startValue = startValue; this.endValue = endValue; this.total = total; this.threadName = threadName; this.resultReceiver = resultReceiver; } static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @Override public void run() { // TODO Auto-generated method stub try{ //开始计算 System.out.println(sdf.format(new Date())+": "+this.threadName+" count start:"+startValue+"+"+(startValue+1)+"+"+(startValue+2)+" .....+"+endValue+"=?"); Thread.sleep(2000); long total = 0; for(int i=startValue;i<endValue;i++){ total+= i; } System.out.println(sdf.format(new Date())+": "+this.threadName+" count finish:"+startValue+"+"+(startValue+1)+"+"+(startValue+2)+" .....+"+endValue+" = "+total); this.total.addAndGet(total);//计算完成 cyclicBarrier.await();//本线程计算工作完成,阻塞等待其它线程完成完成计算 System.out.println(sdf.format(new Date())+": "+this.threadName+" send result to "+resultReceiver+":"+this.total.get());//所有任务计算完成,将最终结果发送给某人 System.out.println(sdf.format(new Date())+": "+this.threadName+" to exit");//线程退出 }catch(Exception e){ e.printStackTrace(); } } public static void main(String args []){ final AtomicLong total = new AtomicLong(); int startValue = 1; int endValue = 10000; int difference = endValue-startValue; int threadNum = difference/1000;//每条线程计算1000个数据相加 if(difference%100>0){//计算需要线程数 threadNum++; } CyclicBarrier cyclicBarrier = new CyclicBarrier(threadNum,new Runnable(){ @Override public void run() {//把有线程任务执行完成时,调用。 // TODO Auto-generated method stub System.out.println(sdf.format(new Date())+": ALL thread count finish , count result ="+total.get()); } }); ExecutorService executorService=Executors.newCachedThreadPool(); for(int i=0;i<threadNum;i++){//任务拆分,发给多个线程执行 int startValueForThread = startValue+(i*1000); int endValueForThread = startValue+(i*1000)+1000; if(endValueForThread>endValue){ endValueForThread= endValue; } TestCyclicBarrier testCyclicBarrier = new TestCyclicBarrier("thread_"+i,cyclicBarrier,total,startValueForThread,endValueForThread,"person"+i); executorService.execute(testCyclicBarrier);//开始执行任务线程 } } }
程序输出:
2013-12-01 22:46:02: thread_1 count start:1001+1002+1003 .....+2001=?
2013-12-01 22:46:02: thread_0 count start:1+2+3 .....+1001=?
2013-12-01 22:46:02: thread_2 count start:2001+2002+2003 .....+3001=?
2013-12-01 22:46:02: thread_3 count start:3001+3002+3003 .....+4001=?
2013-12-01 22:46:02: thread_4 count start:4001+4002+4003 .....+5001=?
2013-12-01 22:46:02: thread_6 count start:6001+6002+6003 .....+7001=?
2013-12-01 22:46:02: thread_5 count start:5001+5002+5003 .....+6001=?
2013-12-01 22:46:02: thread_7 count start:7001+7002+7003 .....+8001=?
2013-12-01 22:46:02: thread_9 count start:9001+9002+9003 .....+10000=?
2013-12-01 22:46:02: thread_8 count start:8001+8002+8003 .....+9001=?
2013-12-01 22:46:04: thread_1 count finish:1001+1002+1003 .....+2001 = 1500500
2013-12-01 22:46:04: thread_0 count finish:1+2+3 .....+1001 = 500500
2013-12-01 22:46:04: thread_2 count finish:2001+2002+2003 .....+3001 = 2500500
2013-12-01 22:46:04: thread_3 count finish:3001+3002+3003 .....+4001 = 3500500
2013-12-01 22:46:04: thread_4 count finish:4001+4002+4003 .....+5001 = 4500500
2013-12-01 22:46:04: thread_5 count finish:5001+5002+5003 .....+6001 = 5500500
2013-12-01 22:46:04: thread_6 count finish:6001+6002+6003 .....+7001 = 6500500
2013-12-01 22:46:04: thread_7 count finish:7001+7002+7003 .....+8001 = 7500500
2013-12-01 22:46:04: thread_9 count finish:9001+9002+9003 .....+10000 = 9490500
2013-12-01 22:46:04: thread_8 count finish:8001+8002+8003 .....+9001 = 8500500
2013-12-01 22:46:04: ALL thread count finish , count result =49995000
2013-12-01 22:46:04: thread_8 send result to person8:49995000
2013-12-01 22:46:04: thread_8 to exit
2013-12-01 22:46:04: thread_1 send result to person1:49995000
2013-12-01 22:46:04: thread_1 to exit
2013-12-01 22:46:04: thread_2 send result to person2:49995000
2013-12-01 22:46:04: thread_2 to exit
2013-12-01 22:46:04: thread_3 send result to person3:49995000
2013-12-01 22:46:04: thread_3 to exit
2013-12-01 22:46:04: thread_5 send result to person5:49995000
2013-12-01 22:46:04: thread_6 send result to person6:49995000
2013-12-01 22:46:04: thread_9 send result to person9:49995000
2013-12-01 22:46:04: thread_0 send result to person0:49995000
2013-12-01 22:46:04: thread_9 to exit
2013-12-01 22:46:04: thread_6 to exit
2013-12-01 22:46:04: thread_5 to exit
2013-12-01 22:46:04: thread_4 send result to person4:49995000
2013-12-01 22:46:04: thread_4 to exit
2013-12-01 22:46:04: thread_7 send result to person7:49995000
2013-12-01 22:46:04: thread_0 to exit
2013-12-01 22:46:04: thread_7 to exit