标签:random 随机 rand 红色 number final agg code rri
介绍几个同步工具类,很简单、常用。
说些废话啊,最近wo在学习的过程中,多用google搜索,此外,查类的时候,会多看Java api 8 的英文文档,觉得收获很多。
1.CyclicBarrier
它允许一组线程在到达一个共同栅栏之前,相互等待,全都到达之后,再一起前进。与Countdown的去别是,它可以设置多个共同的栅栏,而CountDown只能设置一次。
我们看看api中的构造方法:
CyclicBarrier(int parties): parties表示这组线程的数量
CyclicBarrier(int parties, Runnable barrierAction):barrierAction表示的是,在这组线程全都达到共同栅栏后,所触发的任务。这个任务是由最后到达共同栅栏的线程来执行。
下面看代码的例子:
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.List; 4 import java.util.Random; 5 import java.util.concurrent.BrokenBarrierException; 6 import java.util.concurrent.CyclicBarrier; 7
// 这个demo,是多个线程随机产生数字,放在list中,等到所有线程都产生完数字后(共同栅栏),再对所有数字进行求和
// 测试结果,自己运行一下就好! 8 public class CyclicBarrierDemo { 9 10 private CyclicBarrier cyclicBarrier; 11 private List<List<Integer>> partialResults 12 = Collections.synchronizedList(new ArrayList<>()); 13 private Random random = new Random(); 14 private int NUM_PARTIAL_RESULTS; 15 private int NUM_WORKERS; 16 17 class NumberCruncherThread implements Runnable { 18 19 @Override 20 public void run() { 21 String thisThreadName = Thread.currentThread().getName(); 22 List<Integer> partialResult = new ArrayList<>(); 23 24 // Crunch some numbers and store the partial result 25 for (int i = 0; i < NUM_PARTIAL_RESULTS; i++) { 26 Integer num = random.nextInt(10); 27 System.out.println(thisThreadName 28 + ": Crunching some numbers! Final result - " + num); 29 partialResult.add(num); 30 } 31 32 partialResults.add(partialResult); 33 try { 34 System.out.println(thisThreadName 35 + " waiting for others to reach barrier."); 36 cyclicBarrier.await(); 37
// 红色部分添加的目的就是,纯粹是为了展示cyclicbarrier可以多次重复设置共同栅栏 38 System.out.println("No.2 thread name " + thisThreadName); 39 cyclicBarrier.await(); 40 41 System.out.println("No.3 thread name " + thisThreadName); 42 cyclicBarrier.await(); 43 } catch (InterruptedException e) { 44 // ... 45 } catch (BrokenBarrierException e) { 46 // 当await超时,所示await阻塞的线程被中断时,那么所有阻塞的线程将被终止,并报出BrokenBarrierException 47 } 48 } 49 } 50 51 class AggregatorThread implements Runnable { 52 53 @Override 54 public void run() { 55 56 String thisThreadName = Thread.currentThread().getName(); 57 58 System.out.println( 59 thisThreadName + ": Computing sum of " + NUM_WORKERS 60 + " workers, having " + NUM_PARTIAL_RESULTS + " results each."); 61 int sum = 0; 62 63 for (List<Integer> threadResult : partialResults) { 64 System.out.print("Adding "); 65 for (Integer partialResult : threadResult) { 66 System.out.print(partialResult+" "); 67 sum += partialResult; 68 } 69 System.out.println(); 70 } 71 System.out.println(thisThreadName + ": Final result = " + sum); 72 } 73 } 74 75 public void runSimulation(int numWorkers, int numberOfPartialResults) { 76 NUM_PARTIAL_RESULTS = numberOfPartialResults; 77 NUM_WORKERS = numWorkers; 78 79 cyclicBarrier = new CyclicBarrier(NUM_WORKERS, new AggregatorThread()); 80 81 System.out.println("Spawning " + NUM_WORKERS 82 + " worker threads to compute " 83 + NUM_PARTIAL_RESULTS + " partial results each"); 84 85 for (int i = 0; i < NUM_WORKERS; i++) { 86 Thread worker = new Thread(new NumberCruncherThread()); 87 worker.setName("Thread " + i); 88 worker.start(); 89 } 90 } 91 92 public static void main(String[] args) { 93 CyclicBarrierDemo demo = new CyclicBarrierDemo(); 94 demo.runSimulation(5, 3); 95 } 96 }
后续陆续补充。。。
标签:random 随机 rand 红色 number final agg code rri
原文地址:https://www.cnblogs.com/lihao007/p/10311783.html