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

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

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

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

 package com.yzl.dubbo;

 import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore; /**
* 测试CountDownLatch、CyclicBarrier、Semaphore
* 这里的测试请在Main方法中进行测试,在junit会出现问题,原因是:junit是Daemon线程
*
* @author yangzhilong
*
*/
public class MyTest { public static void main(String[] args) {
MyTest myTest = new MyTest();
myTest.testCountDownLatch();
// myTest.testCyclicBarrier1();
// myTest.testCyclicBarrier2();
// myTest.testSemaphore();
} /**
* 测试CountDownLatch
*
*/
@org.junit.Test
public void testCountDownLatch(){
CountDownLatch latch = new CountDownLatch(5); Thread[] threads = new Thread[5];
Thread thread = null; System.out.println("begin.....................," + System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
final int index = i;
thread = new Thread(()->{ System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
//计数器减一
latch.countDown();
try {
//等待计数器变成0
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis()); try {
Thread.sleep(new Random().nextInt(500));
} catch (InterruptedException e1) {
e1.printStackTrace();
} finally {
System.out.println("finally index:" + index);
}
System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis()); });
threads[i] = thread;
}
for (Thread t : threads) {
t.setDaemon(false);
t.start();
}
System.out.println("end..............," + System.currentTimeMillis());
} /**
* 测试CyclicBarrier-1
*/
@org.junit.Test
public void testCyclicBarrier1(){
CyclicBarrier cyclicBarrier = new CyclicBarrier(6); System.out.println("begin.....................," + System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
final int index = i;
new Thread(()->{
System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis()); try {
Thread.sleep(new Random().nextInt(500));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis());
}).start();;
} try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
} System.out.println("end..............," + System.currentTimeMillis());
} /**
* 测试CyclicBarrier-2
* 程序中会重置计数器
*/
@org.junit.Test
public void testCyclicBarrier2(){
CyclicBarrier cyclicBarrier = new CyclicBarrier(6); System.out.println("begin.....................," + System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
final int index = i;
new Thread(()->{
System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("Thread-" + index + ",开始执行!!!," + System.currentTimeMillis()); try {
Thread.sleep(new Random().nextInt(500));
} catch (InterruptedException e1) {
e1.printStackTrace();
} System.out.println("Thread-" + index + ",执行完成!!!," + System.currentTimeMillis());
}).start();;
} try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
} System.out.println("end..............," + System.currentTimeMillis()); //重置计数器
cyclicBarrier.reset(); try {
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("我永远都不能被执行到");
} /**
* 测试Semaphore
*/
@org.junit.Test
public void testSemaphore(){
Semaphore semaphore = new Semaphore(1); System.out.println("begin.....................");
for (int i = 0; i < 5; i++) {
final int index = i;
new Thread(()->{
System.out.println("Thread-" + index + ",准备执行!!!," + System.currentTimeMillis());
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread-" + index + ",开始执行!!!"); try {
Thread.sleep(new Random().nextInt(500));
} catch (InterruptedException e1) {
e1.printStackTrace();
} System.out.println("Thread-" + index + ",执行完成!!!");
semaphore.release();
}).start();
}
System.out.println("end..............");
try {
Thread.sleep(100000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

运行结果:

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,执行完成!!!

JAVA中的CountDownLatch、CyclicBarrier、Semaphore的简单测试的更多相关文章

  1. Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo

    Java并发编程工具类 CountDownLatch CyclicBarrier Semaphore使用Demo CountDownLatch countDownLatch这个类使一个线程等待其他线程 ...

  2. 并发包下常见的同步工具类详解(CountDownLatch,CyclicBarrier,Semaphore)

    目录 1. 前言 2. 闭锁CountDownLatch 2.1 CountDownLatch功能简介 2.2 使用CountDownLatch 2.3 CountDownLatch原理浅析 3.循环 ...

  3. CountDownLatch/CyclicBarrier/Semaphore 使用过吗?

    CountDownLatch/CyclicBarrier/Semaphore 使用过吗?下面详细介绍用法: 一,(等待多线程完成的)CountDownLatch  背景; countDownLatch ...

  4. 并发包下常见的同步工具类(CountDownLatch,CyclicBarrier,Semaphore)

    在实际开发中,碰上CPU密集且执行时间非常耗时的任务,通常我们会选择将该任务进行分割,以多线程方式同时执行若干个子任务,等这些子任务都执行完后再将所得的结果进行合并.这正是著名的map-reduce思 ...

  5. Java中关于CountDownLatch的使用

    CyclicBarrier工具类主要是控制多个线程的一起执行,演示程序: import java.util.Random; import java.util.concurrent.CountDownL ...

  6. 用好Java中的枚举真的没有那么简单

    1.概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承j ...

  7. Java中的4个并发工具类 CountDownLatch CyclicBarrier Semaphore Exchanger

    在 java.util.concurrent 包中提供了 4 个有用的并发工具类 CountDownLatch 允许一个或多个线程等待其他线程完成操作,课题点 Thread 类的 join() 方法 ...

  8. 转发---[沧海拾遗]java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch CountDownLatch是一个计 ...

  9. 多线程中 CountDownLatch CyclicBarrier Semaphore的使用

    CountDownLatch 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行.也可以传入时间,表示时间到之后,count还没有为0的时候,就会继续执行. package ...

随机推荐

  1. Ubuntu 16.04 grub rescue 模式下修复 grub

      前几天整理了下电脑的分区,合并并删除一些分区,结果导致 grub 被破坏了,Ubuntu进不去了,启动后直接进入了 rescure 模式.后来又折腾了下,终于修复好了,现总结一下. 先说一下我的系 ...

  2. ZMQ和MessagePack的简单使用(转)

    近段日子在做一个比较复杂的项目,其中用到了开源软件ZMQ和MessagePack.ZMQ对底层网络通信进行了封装,是一个消息处理队列库, 使用起来非常方便.MessagePack是一个基于二进制的对象 ...

  3. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  4. Gson Json 序列号 最常用的功能 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. JS获取当前项目名

    代码如下: //获取当前网址,如: var curWwwPath=window.document.location.href; //获取主机地址之后的目录如:/Tmall/index.jsp var ...

  6. python3 验证码图片切割

    切割前图片 切割后四个图片 代码 #coding:utf8 import os from PIL import Image,ImageDraw,ImageFile import numpy impor ...

  7. centos6安装ElasticSearch5.6.5错误记录

    在centos6安装ES的时候遇到不少问题.这里记录日志.以后安装一定要升级centos7,别说啥稳定问题了,该升级就升级. [1]: max file descriptors [4096] for ...

  8. WebApi&MVC对比

    使用上区分,mvc主要用于建站,web api主要用于构建http服务,当然你非要用mvc来构建Uri式的Api也行,不过显然是没有这个必要的,一个不恰当的比喻就像是你也可以玩破解版的单机游戏,也可以 ...

  9. 使用nginx反向代理到不同服务器(共享同一端口)配置文件

    使用nginx反向代理到不同服务器(共享同一端口)配置文件 https://blog.csdn.net/wang_k_123/article/details/72779443 https://www. ...

  10. create-react-app时registry的奇怪问题

    用React官方给的NPM脚本 create-react-app my-app 在自动安装module的过程中,在安装registry的组件的时候莫名其妙的挂住不动了.界面显示的信息如下: fetch ...