• CyclicBarrier 使用说明


    字面意思回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行。叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用。
     
    主要方法:
         public int await() throws InterruptedException, BrokenBarrierException
         用来挂起当前线程,直至所有线程都到达barrier状态再同时执行后续任务。
     
    例子:
         跟团旅游,没到一个景点导游给介绍完后,让大家自由活动,然后约定好(某个时间)在下一个景点集合。
      
     1 import java.util.concurrent.CyclicBarrier;
     2 import java.util.concurrent.ExecutorService;
     3 import java.util.concurrent.Executors;
     4  
     5 public class CyclicBarrierTest {
     6  
     7         public static void main(String[] args) {
     8               ExecutorService service = Executors. newCachedThreadPool();
     9                final CyclicBarrier cb = new CyclicBarrier(3);
    10                for(int i = 0; i < 3; i++){
    11                      Runnable runnable = new Runnable(){
    12                             public void run(){
    13                                    try {
    14                                          Thread. sleep((long)(Math. random()*10000));     
    15                                          System. out.println("线程" + Thread.currentThread().getName() +
    16                                                         "即将到达集合地点1,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候" ));                                        
    17                                          cb.await();
    18                                          
    19                                          Thread. sleep((long)(Math. random()*10000));     
    20                                          System. out.println("线程" + Thread.currentThread().getName() +
    21                                                         "即将到达集合地点2,当前已有" + (cb.getNumberWaiting()+1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候" ));
    22                                          cb.await();   
    23                                          Thread. sleep((long)(Math. random()*10000));     
    24                                          System. out.println("线程" + Thread.currentThread().getName() +
    25                                                         "即将到达集合地点3,当前已有" + (cb.getNumberWaiting() + 1) + "个已经到达," + (cb.getNumberWaiting()==2?"都到齐了,继续走啊":"正在等候" ));                                      
    26                                          cb.await();                                     
    27                                   } catch (Exception e) {
    28                                          e.printStackTrace();
    29                                   }                          
    30                            }
    31                      };
    32                      service.execute(runnable);
    33               }
    34               service.shutdown();
    35        }
    36 }
  • 相关阅读:
    周赛F题 POJ 1458(最长公共子序列)
    HDU 4720 Naive and Silly Muggles 2013年四川省赛题
    HDU 4716 A Computer Graphics Problem 2013年四川省赛题
    SCU 4440 Rectangle 2015年四川省赛题
    SCU 4436 Easy Math 2015年四川省赛题
    大数模板——六种实现了加减乘除和求余
    HDU 1002 A + B Problem II
    CodeForces 689C  Mike and Chocolate Thieves
    CodeForces 689A -Mike and Cellphone
    CodeForces 595B
  • 原文地址:https://www.cnblogs.com/tstd/p/4999806.html
Copyright © 2020-2023  润新知