一、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. KVM-克隆和快照管理

    kvm 虚拟机有两部分组成:img镜像文件和xml配置文件 /etc/libvirt/qemu #xml配置文件目录,存在虚拟机所有的详细信息 1.kvm虚拟机克隆 克隆命令 virt-clone - ...

  2. sql几种删除语句的联系与区别

    DELETE.TRUNCATE.DROP三种删除语句联系与区别 相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是D ...

  3. 【ASP.NET】IHttpHandler和IHttpModule

    上篇文章我们主要讲了HttpApplicatiion管道事件,那么我么如何处理这些管道事件呢,以及请求在ASP.NET是如何执行的呢,我们来了解一下IHttpHandler和IHttpModule 引 ...

  4. 从零开始做SSH项目(一)

    1.数据库脚本 用户表 CREATE TABLE `ybl`.`userinfo`( `id` INT NOT NULL AUTO_INCREMENT, `email` ) NOT NULL, `id ...

  5. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  6. 对StackOverflow上投票最高的带javascript标签的问题的汇总

    https://github.com/simongong/js-stackoverflow-highest-votes

  7. getResourceAsStream 地址

    getResourceAsStream ()返回的是inputstream getResource()返回:URL Class.getResource("")    返回的是当前C ...

  8. 洛谷P2782 友好城市

    题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.没对友好城市都向政府申请在河上开辟一条直线航 ...

  9. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  10. [BZOJ4898] [Apio2017]商旅

    [BZOJ4898] [Apio2017]商旅 传送门 试题分析 考虑两个点之间的路径,显然如果交易的话肯定选\(S_{t,i}-B_{s,i}\)最大的. 那么我们可以先用\(Cost\)把两个点的 ...