首页 技术 正文
技术 2022年11月15日
0 收藏 890 点赞 4,542 浏览 2592 个字

一、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();
}
}
}

运行截图:

并发编程JUC系列AQS(CountDownLatch、CyclicBarrier、Semaphore)

二、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();
}
}

运行截图:

并发编程JUC系列AQS(CountDownLatch、CyclicBarrier、Semaphore)

三、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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896