
CyclicBarrier可以用于多线程计算数据,最后合并计算结果的场景。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import java.util.Random; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
public class implements Runnable { private static ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>(); private static CyclicBarrier barrier; public static void main(String[] args) throws Exception { barrier = new CyclicBarrier(5, new Runnable() { public void run() { System.out.println("所有得分得到!"); int result = 0; for (String key : map.keySet()) { result += map.get(key); } System.out.println("平均值为:" + (result/5)); } });
ExecutorService es = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { es.execute(new Shit()); } }
public void run() { try { int score = new Random().nextInt(100); map.put("科目" + Thread.currentThread().getName() , score); System.out.println("科目" + Thread.currentThread().getName() + "已获得得分: " + score); Thread.sleep(1000); barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
|
只有当五个线程都结束时,才会进入计算平均值的方法中,CyclicBarrier起到了一个栅栏作用,一旦线程都达到了栅栏处,就会进入闸门方法中。
近期评论