JavaSE--CountDownLatch & CyclicBarrier

Posted MicroCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSE--CountDownLatch & CyclicBarrier相关的知识,希望对你有一定的参考价值。

参考:http://www.importnew.com/21889.html

CountDownLatch

  countDown() 方法执行完只是计数器减一, 并不会阻塞当前运行线程的的后续代码执行.

 

 

 1 package org.wzh.demo.demo1;
 2 
 3 import java.util.Random;
 4 import java.util.concurrent.CountDownLatch;
 5 
 6 public class D4CountDownLatch {
 7 
 8     class Task extends Thread {
 9 
10         private CountDownLatch latch;
11 
12         public Task(CountDownLatch latch) {
13             super();
14             this.latch = latch;
15         }
16 
17         @Override
18         public void run() {
19             // TODO Auto-generated method stub
20             try {
21                 int time = (new Random().nextInt(8000) + 10000) / 1000;
22                 System.out.println(Thread.currentThread().getName() + " 预计成绩 " + time + " seconds");
23                 Thread.sleep(time * 1000);
24                 latch.countDown();//***
25                 System.out.println(Thread.currentThread().getName() + " 到达终点!");
26                 synchronized (latch) {
27                     System.out.println("~~~~~~~~");
28                     System.out.println(Thread.currentThread().getName());
29                     System.out.println("~~~~~~~~");
30                 }
31             } catch (InterruptedException e) {
32                 // TODO Auto-generated catch block
33                 e.printStackTrace();
34             }
35             
36         }
37 
38     }
39 
40     public static void main(String[] args) throws InterruptedException {
41         CountDownLatch latch = new CountDownLatch(3);//***
42 
43         System.out.println("百米赛跑比赛开始");
44 
45         System.out.println("参赛选手:");
46         String[] names = {"小明", "小强", "小智"};
47         for (String name : names) {
48             System.out.println("	" + name);
49         }
50         
51         for (int i = 0; i < 3; i++) {
52             Thread t = new D4CountDownLatch().new Task(latch);
53             t.setName(names[i]);
54             t.start();
55         }
56         latch.await();//***
57         Thread.sleep(500);
58         System.out.println("百米赛跑比赛结束");
59     }
60 
61 }
百米赛跑比赛开始
参赛选手:
    小明
    小强
    小智
小智 预计成绩 10 seconds
小明 预计成绩 14 seconds
小强 预计成绩 14 seconds
小智 到达终点!
~~~~~~~~
小智
~~~~~~~~
小强 到达终点!
~~~~~~~~
小强
小明 到达终点!
~~~~~~~~
~~~~~~~~
小明
~~~~~~~~
百米赛跑比赛结束

 

 

CyclicBarrier

  await() 方法阻塞后面的代码,直到达到条件才继续向下执行.  

 

 1 package org.wzh.demo.demo1;
 2 
 3 import java.util.Random;
 4 import java.util.concurrent.BrokenBarrierException;
 5 import java.util.concurrent.CyclicBarrier;
 6 
 7 
 8 public class D4CyclicBarrier {
 9     
10     class Task extends Thread {
11 
12         private CyclicBarrier barrier;
13 
14         public Task(CyclicBarrier barrier) {
15             super();
16             this.barrier = barrier;
17         }
18 
19         @Override
20         public void run() {
21             // TODO Auto-generated method stub
22             try {
23                 int time = (new Random().nextInt(8000) + 10000) / 1000;
24                 System.out.println(Thread.currentThread().getName() + " 预计成绩 " + time + " seconds");
25                 Thread.sleep(time * 1000);
26                 System.out.println(Thread.currentThread().getName() + " 到达终点!");
27                 barrier.await();//***
28                 synchronized (barrier) {
29                     System.out.println("~~~~~~~~");
30                     System.out.println(Thread.currentThread().getName());
31                     System.out.println("~~~~~~~~");
32                 }
33             } catch (InterruptedException e) {
34                 // TODO Auto-generated catch block
35                 e.printStackTrace();
36             } catch (BrokenBarrierException e) {
37                 // TODO Auto-generated catch block
38                 e.printStackTrace();
39             }
40         }
41 
42     }
43     
44     public static void main(String[] args) {
45         try {
46             CyclicBarrier barrier = new CyclicBarrier(4);//***
47 
48             System.out.println("百米赛跑比赛开始");
49             String[] names = {"小明", "小强", "小智"};
50             System.out.println("参赛选手:");
51             for (String name : names) {
52                 System.out.println("	" + name);
53             }
54             for (int i = 0; i < 3; i++) {
55                 Thread t = new D4CyclicBarrier().new Task(barrier);
56                 t.setName(names[i]);
57                 t.start();
58             }
59             barrier.await();
60             Thread.sleep(500);
61             System.out.println("百米赛跑比赛结束");
62         } catch (Exception e) {
63             e.printStackTrace();
64         }
65     }
66 
67 }
百米赛跑比赛开始
参赛选手:
    小明
    小强
    小智
小明 预计成绩 12 seconds
小强 预计成绩 14 seconds
小智 预计成绩 10 seconds
小智 到达终点!
小明 到达终点!
小强 到达终点!
~~~~~~~~
小智
~~~~~~~~
~~~~~~~~
小明
~~~~~~~~
~~~~~~~~
小强
~~~~~~~~
百米赛跑比赛结束

 

 

区别

  CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同:

    CountDownLatch一般用于某个线程A等待若干个其他线程执行完任务之后,它才执行;

    而CyclicBarrier一般用于一组线程互相等待至某个状态,然后这一组线程再同时执行;

  另外,CountDownLatch是不能够重用的,而CyclicBarrier是可以重用的。

以上是关于JavaSE--CountDownLatch & CyclicBarrier的主要内容,如果未能解决你的问题,请参考以下文章

20170924 a20170924 a

a++和++a的区别

逻辑代数

python运算符

运算符

arraylist排序 例如值 a104,a106,a102,a92,a98,a94 结果a92,a94,a98,a102,a104,a106