JAVA中的CountDownLatch、CyclicBarrier、Semaphore的简单测试

因公司需要做一个对于CountDownLatch的分享,特写了此blog。

具体细节可以参见:小结java自带的跟锁相关的一些类

 在做这个分享的过程中发现了Main和junit的运行的区别,在另外一个Blog细说。

详见:http://www.cnblogs.com/yangzhilong/p/6840791.html

  1 package com.yzl.dubbo;
  2 
  3 import java.util.Random;
  4 import java.util.concurrent.BrokenBarrierException;
  5 import java.util.concurrent.CountDownLatch;
  6 import java.util.concurrent.CyclicBarrier;
  7 import java.util.concurrent.Semaphore;
  8 
  9 /**
 10  * 测试CountDownLatch、CyclicBarrier、Semaphore
 11  * 这里的测试请在Main方法中进行测试,在junit会出现问题,原因是:junit是Daemon线程
 12  * 
 13  * @author yangzhilong
 14  *
 15  */
 16 public class MyTest {
 17     
 18     public static void main(String[] args) {
 19         MyTest myTest = new MyTest();
 20         myTest.testCountDownLatch();
 21 //        myTest.testCyclicBarrier1();
 22 //        myTest.testCyclicBarrier2();
 23 //        myTest.testSemaphore();
 24     }
 25         
 26     /**
 27      * 测试CountDownLatch
 28      * 
 29      */
 30     @org.junit.Test
 31     public void testCountDownLatch(){
 32         CountDownLatch latch = new CountDownLatch(5);
 33         
 34         Thread[] threads = new Thread[5];
 35         Thread thread = null;
 36         
 37         System.out.println("begin.....................," + System.currentTimeMillis());
 38         for (int i = 0; i < 5; i++) {
 39             final int index = i;
 40             thread = new Thread(()->{
 41                 
 42                 System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
 43                 //计数器减一
 44                 latch.countDown();
 45                 try {
 46                     //等待计数器变成0
 47                     latch.await();
 48                 } catch (InterruptedException e) {
 49                     e.printStackTrace();
 50                 }
 51                 System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis());
 52                 
 53                 try {
 54                     Thread.sleep(new Random().nextInt(500));
 55                 } catch (InterruptedException e1) {
 56                     e1.printStackTrace();
 57                 } finally {
 58                     System.out.println("finally index:" + index);
 59                 }
 60                 System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis());
 61                 
 62             });
 63             threads[i] = thread;
 64         }
 65         for (Thread t : threads) {
 66             t.setDaemon(false);
 67             t.start();
 68         }
 69         System.out.println("end..............," + System.currentTimeMillis());
 70     }
 71     
 72     /**
 73      * 测试CyclicBarrier-1
 74      */
 75     @org.junit.Test
 76     public void testCyclicBarrier1(){
 77         CyclicBarrier cyclicBarrier = new CyclicBarrier(6);
 78         
 79         System.out.println("begin.....................," + System.currentTimeMillis());
 80         for (int i = 0; i < 5; i++) {
 81             final int index = i;
 82             new Thread(()->{
 83                 System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
 84                 try {
 85                     cyclicBarrier.await();
 86                 } catch (InterruptedException | BrokenBarrierException e) {
 87                     e.printStackTrace();
 88                 }
 89                 System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis());
 90                 
 91                 try {
 92                     Thread.sleep(new Random().nextInt(500));
 93                 } catch (InterruptedException e1) {
 94                     e1.printStackTrace();
 95                 }
 96                 System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis());
 97             }).start();;
 98         }
 99 
100         try {
101             cyclicBarrier.await();
102         } catch (InterruptedException | BrokenBarrierException e) {
103             e.printStackTrace();
104         }
105 
106         System.out.println("end..............," + System.currentTimeMillis());
107     }
108     
109     /**
110      * 测试CyclicBarrier-2
111      * 程序中会重置计数器
112      */
113     @org.junit.Test
114     public void testCyclicBarrier2(){
115         CyclicBarrier cyclicBarrier = new CyclicBarrier(6);
116         
117         System.out.println("begin.....................," + System.currentTimeMillis());
118         for (int i = 0; i < 5; i++) {
119             final int index = i;
120             new Thread(()->{
121                 System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
122                 try {
123                     cyclicBarrier.await();
124                 } catch (InterruptedException | BrokenBarrierException e) {
125                     e.printStackTrace();
126                 }
127                 System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis());
128                 
129                 try {
130                     Thread.sleep(new Random().nextInt(500));
131                 } catch (InterruptedException e1) {
132                     e1.printStackTrace();
133                 }
134                 
135                 System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis());
136             }).start();;
137         }
138 
139         try {
140             cyclicBarrier.await();
141         } catch (InterruptedException | BrokenBarrierException e) {
142             e.printStackTrace();
143         }
144 
145         System.out.println("end..............," + System.currentTimeMillis());
146         
147         //重置计数器
148         cyclicBarrier.reset();
149         
150         try {
151             cyclicBarrier.await();
152         } catch (InterruptedException | BrokenBarrierException e) {
153             e.printStackTrace();
154         }
155         System.out.println("我永远都不能被执行到");
156     }
157     
158     /**
159      * 测试Semaphore
160      */
161     @org.junit.Test
162     public void testSemaphore(){
163         Semaphore semaphore = new Semaphore(1);
164         
165         System.out.println("begin.....................");
166         for (int i = 0; i < 5; i++) {
167             final int index = i;
168             new Thread(()->{
169                 System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
170                 try {
171                     semaphore.acquire();
172                 } catch (InterruptedException e) {
173                     e.printStackTrace();
174                 }
175                 System.out.println("Thread-" + index + ",开始执行!!!");
176                 
177                 try {
178                     Thread.sleep(new Random().nextInt(500));
179                 } catch (InterruptedException e1) {
180                     e1.printStackTrace();
181                 }
182                 
183                 System.out.println("Thread-" + index + ",执行完成!!!");
184                 semaphore.release();
185             }).start();
186         }
187         System.out.println("end..............");
188         try {
189             Thread.sleep(100000L);
190         } catch (InterruptedException e) {
191             e.printStackTrace();
192         }
193     }
194 }

 

运行结果:

begin.....................,1494483205995
end..............,1494483206092
Thread-0,准备执行!!!,1494483206094
Thread-2,准备执行!!!,1494483206094
Thread-3,准备执行!!!,1494483206094
Thread-4,准备执行!!!,1494483206094
Thread-1,准备执行!!!,1494483206094
Thread-1,开始执行!!!,1494483206094
Thread-2,开始执行!!!,1494483206095
Thread-0,开始执行!!!,1494483206095
Thread-3,开始执行!!!,1494483206095
Thread-4,开始执行!!!,1494483206095
finally index:4
Thread-4,执行完成!!!,1494483206260
finally index:1
Thread-1,执行完成!!!,1494483206390
finally index:0
Thread-0,执行完成!!!,1494483206483
finally index:3
Thread-3,执行完成!!!,1494483206548
finally index:2
Thread-2,执行完成!!!,1494483206567
begin.....................,1494483641348
Thread-0,准备执行!!!,1494483641454
Thread-1,准备执行!!!,1494483641454
Thread-2,准备执行!!!,1494483641454
Thread-3,准备执行!!!,1494483641455
Thread-4,准备执行!!!,1494483641455
end..............,1494483641455
Thread-1,开始执行!!!,1494483641456
Thread-0,开始执行!!!,1494483641456
Thread-2,开始执行!!!,1494483641456
Thread-3,开始执行!!!,1494483641456
Thread-4,开始执行!!!,1494483641456
Thread-3,执行完成!!!,1494483641502
Thread-0,执行完成!!!,1494483641509
Thread-4,执行完成!!!,1494483641545
Thread-1,执行完成!!!,1494483641564
Thread-2,执行完成!!!,1494483641692
begin.....................,1494483670144
Thread-0,准备执行!!!,1494483670268
Thread-1,准备执行!!!,1494483670268
Thread-2,准备执行!!!,1494483670268
Thread-3,准备执行!!!,1494483670268
Thread-4,准备执行!!!,1494483670268
Thread-4,开始执行!!!,1494483670269
Thread-3,开始执行!!!,1494483670269
Thread-2,开始执行!!!,1494483670269
Thread-0,开始执行!!!,1494483670269
Thread-1,开始执行!!!,1494483670269
end..............,1494483670269
Thread-3,执行完成!!!,1494483670399
Thread-2,执行完成!!!,1494483670404
Thread-1,执行完成!!!,1494483670486
Thread-0,执行完成!!!,1494483670536
Thread-4,执行完成!!!,1494483670553
begin.....................
Thread-0,准备执行!!!,1494483690182
end..............
Thread-0,开始执行!!!
Thread-1,准备执行!!!,1494483690183
Thread-2,准备执行!!!,1494483690183
Thread-3,准备执行!!!,1494483690184
Thread-4,准备执行!!!,1494483690184
Thread-0,执行完成!!!
Thread-1,开始执行!!!
Thread-1,执行完成!!!
Thread-2,开始执行!!!
Thread-2,执行完成!!!
Thread-3,开始执行!!!
Thread-3,执行完成!!!
Thread-4,开始执行!!!
Thread-4,执行完成!!!

 

posted @ 2017-03-28 15:54  自行车上的程序员  阅读(384)  评论(0编辑  收藏  举报