Java5 多线程(五)--CyclicBarrier同步的工具类


允许一系列的集合等待彼此,到达一个共同的障碍物点.
表示大家彼此等待,大家集合好后才开始出发,分散活动后又在指定的地点集合碰面,这就好比整个公司的人员利用周末时间集体郊游一样,先各自从家里出发到公司集合后,在同时出发到公园游玩,在指定的地点后再同时开始就餐,...
public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        final CyclicBarrier cb = new CyclicBarrier(3);
        for (int i = 0; i < 3; i++) {
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程"
                                + Thread.currentThread().getName()
                                + "即将到达集合地点1,当前已有"
                                + (cb.getNumberWaiting() + 1)
                                + "个已经到达,"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
                                        : "正在等候"));
                         cb.await();//只有三个线程都到齐了才往下走
 
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程"
                                + Thread.currentThread().getName()
                                + "即将到达集合地点2,当前已有"
                                + (cb.getNumberWaiting() + 1)
                                + "个已经到达,"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
                                        : "正在等候"));
 
                        cb.await();
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("线程"
                                + Thread.currentThread().getName()
                                + "即将到达集合地点3,当前已有"
                                + (cb.getNumberWaiting() + 1)
                                + "个已经到达,"
                                + (cb.getNumberWaiting() == 2 ? "都到齐了,继续走啊"
                                        : "正在等候"));
 
                        cb.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            service.execute(runnable);
        }
        service.shutdown();
    }

Java5 多线程(五)--CyclicBarrier同步的工具类_第1张图片

在Api有一个使用CyclicBarrier的例子:
class Solver {
   final int N;
   final float[][] data;
   final CyclicBarrier barrier;

   class Worker implements  Runnable {
     int myRow;
     Worker(int row) { myRow = row; }
     public void run() {
       while (!done()) {
         processRow(myRow);

         try {
           barrier.await ();//处理完一行在此等待,如果N个线程都完成了各自的行,都在此等待,然后
将会执行new CyclicBarrier(N,Runnbale)里面Runnable的代码,也就是合并行mergeRows();
         } catch (InterruptedException ex) {
           return;
         } catch (BrokenBarrierException ex) {
           return;
         }
       }
     }
   }

   public Solver(float[][] matrix) {
     data = matrix;
     N = matrix.length;
     barrier = new CyclicBarrier(N,
                                 new Runnable() {
                                   public void run() {
                                     mergeRows(...);
                                   }
                                 });
     for (int i = 0; i < N; ++i)
       new Thread(new Worker(i)).start();

     waitUntilDone();
   }
}
在这里每个工作者(Worker)线程处理这个矩阵一行,然后在这个障碍物处等待,知道矩阵的所有行都被处理了,当所有的行都被处理了提供的Runnable障碍物被执行并且合并这些行,如果合并者确定了一个合并方案,然后将会返回(return),并且每个worker将终止.

转载请注明出处: http://blog.csdn.net/johnny901114/article/details/8695723

你可能感兴趣的:(Java5 多线程(五)--CyclicBarrier同步的工具类)