一、CountDownLatch

package com.jonychen.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* 并发编程java.util.concurrent(JUC)
* AQS(AbstractQueuedSynchronizer)
*/
public class JUCAQSDemo {
public static void main(String[] args){
/**
* CountDownLatch用来控制一个线程等待多个线程,维护一个计数器cnt,
* 每次调用countDown()会让计数器的值减一, 减到零时,
* 那些因为调用await()方法而在等待的线程会被唤醒
*/
final int totalThread=10;
CountDownLatch countDownLatch =new CountDownLatch(totalThread);
ExecutorService executorService =Executors.newCachedThreadPool();
for (int i = 0; i < totalThread; i++) {
executorService.execute(()->{
System.out.println("jonychen run");
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
System.out.println("end...");
executorService.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

运行截图:

二、CyclicBarrier

 package com.jonychen.thread;

 import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* 并发编程java.util.concurrent(JUC)
* AQS(AbstractQueuedSynchronizer)
*/
public class JUCAQSCycliBarrier {
/**
*CyclicBarrier用来控制多个线程相互等待,只有当多个线程都到达时,这些线程才会继续执行,
* 和CountDownLatch相似,都是通过维护计数器实现的,但他的计数器是递增的。每次执行await()
* 方法后,计数器会加1,直到计数器的值和设置的值相同,等待的所有线程才会继续执行,和CountDownLatch
* 的另一个区别是,CyclicBarrier的计数器可以循环使用,所以才叫他循环屏障
*/
public static void main(String[] args){
final int totalThread=10;
CyclicBarrier cyclicBarrier =new CyclicBarrier(totalThread);
ExecutorService executorService=Executors.newCachedThreadPool();
for (int i = 0; i < totalThread; i++) {
executorService.execute(()->{
System.out.println("before..*");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.print("after ");
});
}
executorService.shutdown();
}
}

运行截图:

三、Semaphore

package com.jonychen.thread;

import sun.misc.Cleaner;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; /**
* 并发编程java.util.concurrent(JUC)
* AQS(AbstractQueuedSynchronizer)
*/
public class JUCAQSSemaphore { public static void main(String[] args){
/**
*Semaphore就是操作系统中的信号量,可以控制对互斥资源的访问线程数
*以下代码模拟了对某个服务的并发请求,每次只能有30个客户端同时访问,请求总数为 10。
*/
final int clientCount=30;
final int totalRequestCount=10;
Semaphore semaphore =new Semaphore(clientCount);
ExecutorService executorService=Executors.newCachedThreadPool();
for (int i = 0; i < totalRequestCount; i++) {
executorService.execute(()->{
try {
semaphore.acquire();
System.out.println(semaphore.availablePermits() + "");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();
}
});
}
executorService.shutdown();
}
}

运行截图:

并发编程JUC系列AQS(CountDownLatch、CyclicBarrier、Semaphore)的更多相关文章

  1. 并发工具类的使用 CountDownLatch,CyclicBarrier,Semaphore,Exchanger

    1.CountDownLatch 允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助. A CountDownLatch用给定的计数初始化. await方法阻塞,直到由于countDo ...

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

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

  3. java并发编程JUC第九篇:CountDownLatch线程同步

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口.ArrayBlockingQueue.DelayQueue.LinkedBlockingQueue.Priorit ...

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

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

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

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

  6. Java并发编程锁系列之ReentrantLock对象总结

    Java并发编程锁系列之ReentrantLock对象总结 在Java并发编程中,根据不同维度来区分锁的话,锁可以分为十五种.ReentranckLock就是其中的多个分类. 本文主要内容:重入锁理解 ...

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

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

  8. 【Java并发编程实战】----- AQS(三):阻塞、唤醒:LockSupport

    在上篇博客([Java并发编程实战]----- AQS(二):获取锁.释放锁)中提到,当一个线程加入到CLH队列中时,如果不是头节点是需要判断该节点是否需要挂起:在释放锁后,需要唤醒该线程的继任节点 ...

  9. 高并发第十单:J.U.C AQS(AbstractQueuedSynchronizer) 组件:CountDownLatch. CyclicBarrier .Semaphore

    这里有一篇介绍AQS的文章 非常好: Java并发之AQS详解 AQS全名:AbstractQueuedSynchronizer,是并发容器J.U.C(java.lang.concurrent)下lo ...

随机推荐

  1. 搭建openstack系统初始化(2)

    操作系统环境 :Centos 7.3 x64 1).安装需要的包 yum install wget vim chrony net-tools bash-completion -y 2)配置阿里elpl ...

  2. Linux操作命令(四)

    本次实验将介绍 Linux 命令中 which.whereis.locate 命令的用法. which whereis locate 1.which which命令的作用是,在PATH变量指定的路径中 ...

  3. Xamarin.Forms教程下载安装JDK配置环境变量

    Xamarin.Forms教程下载安装JDK配置环境变量 Xamarin.Form环境配置下载安装JDK JDK是编程Java程序必须的软件.也许有人会问我们用的C#为什么还有Java呢?这是因为我们 ...

  4. 【BZOJ 1004】【HNOI 2008】Cards

    http://www.lydsy.com/JudgeOnline/problem.php?id=1004 注意数据给出的m是一个没有单位元的置换群! 用Burnside引理,然后对每个置换群dp一下就 ...

  5. luogu P3375 【模板】KMP字符串匹配

    题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...

  6. AtCoder - 3939 Strange Nim

    Problem Statement Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles o ...

  7. Deep TEN: Texture Encoding Network

    纹理特征,材料分类(Material Classification),在MINC-2500.Flickr Material Database.KTH-TIPS-2b.4D-Light-Field-Ma ...

  8. 【MySQL笔记】启动弹窗问题,unable to connect to remote host. catalog download has failed.

    安装完MySQL之后,它每天凌晨启动一个Intaller任务,甚是烦人:   这是一个Windows的计划服务,在这里删除即可,开始/附件/系统工具/任务计划程序,把mysql的定时任务计划取消/删除 ...

  9. Android Toolbar返回按钮颜色修改

    // 代码设置toolbar返回键颜色为白色 val upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mater ...

  10. ubuntu系统使用dnw下载程序

    转:http://blog.chinaunix.net/uid-22030783-id-3350840.html 获得dnw4linux.tar.bz2 源码包,可在xcembed论坛下载:http: ...