当前位置:网站首页>JUC powerful auxiliary class
JUC powerful auxiliary class
2022-07-24 08:53:00 【linsa_ pursuer】
JUC Powerful auxiliary classes
1. Reduce the count CountDownLatch
2. Recycling fence CyclicBarrier
3. Signal lamp Semaphore
Reduce the count CountDownLatch demonstration
package juc;
import java.util.concurrent.CountDownLatch;
// demonstration CountDownLatch
public class CountDownLatchDemo {
//6 After the students left the classroom one after another , The monitor locked the door
public static void main(String[] args) throws InterruptedException {
// establish CountDownLatch object , Set initial value
CountDownLatch countDownLatch = new CountDownLatch(6);
//6 After the students left the classroom one after another
for (int i=1; i <=6; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+" Classmate No. left the classroom ");
// Count -1
countDownLatch.countDown();
},String.valueOf(i)).start();
}
// wait for ( Turn into 0 Wait for the end )
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+" The monitor locked the door and left ");
}
}
Recycling fence CyclicBarrier demonstration
package juc;
import java.util.concurrent.CyclicBarrier;
// Gather together 7 A dragon ball can summon a dragon
public class CyclicBarrierDemo {
// Create a fixed value
private static final int NUMBER = 7;
public static void main(String[] args) {
// establish CyclicBarrier
CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, ()->{
System.out.println(" Gather together 7 A dragon ball can summon a dragon ");
});
// Gather together 7 Dragon Ball process
for (int i=1; i <=7; i++) {
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" The dragon balls were collected ");
// wait for
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
Signal lamp Semaphore demonstration
package juc;
import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
//6 car , stop 3 A parking space
public class SemaphoreDemo {
public static void main(String[] args) {
// establish Semaphore, Set the number of licenses
Semaphore Semaphore = new Semaphore(3);
// simulation 6 car
for (int i=1; i <=6; i++) {
new Thread(()->{
try {
// preemption
Semaphore.acquire();
System.out.println(Thread.currentThread().getName()+" Got the parking space ");
// Set random parking time
TimeUnit.SECONDS.sleep(new Random().nextInt(5));
System.out.println(Thread.currentThread().getName()+" Leaving the parking space ");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Release
Semaphore.release();
}
},String.valueOf(i)).start();
}
}
}
边栏推荐
- pip3 带源安装大全
- SQL learning
- Virtual machine terminator terminal terminator installation tutorial
- 利用opencv 做一个简单的人脸识别
- Houdini notes
- Beandefinition three ways to load beans
- Typescript -- Generic
- Wargames bandit (11-20) problem solving essay
- How to configure env.js in multiple environments in uni app
- Kotlin learning note 1 - variables, functions
猜你喜欢

Realize page return to parent directory based on cookies

脉脉网友出了道 Go 面试题,你能答对吗?

Some mistakes that Xiaobai often makes (update from time to time, and promise not to make them again next time)

Sed add content after a line

First acquaintance with JVM

Web3≠NFT? A digital Renaissance?

How to integrate and use log4net logging plug-in in vs2019 class library

3587. 连通图(吉林大学考研机试题)

Wargames NATAS (11-15) problem solving essay

WordPress free theme: document, making reading more convenient
随机推荐
情人节
Run little turtle to test whether the ROS environment in the virtual machine is complete
如何将CAD文件导入图新地球中,与影像地形倾斜模型准确叠加
The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software
How to configure env.js in multiple environments in uni app
【一起上水硕系列】Final RAD-new literacies
Unity C#工具类 ArrayHelper
[FFH] websocket practice of real-time chat room
Basic use of Nacos (2) -- Nacos configuration center
Cyclic multiple scatter
On the relationship between C language function name and function pointer
阻塞队列BlockingQueue 源码解析(ArrayBQ和LinkedBQ)
在npm上发布自己的库
Super complete summary: how to operate files in go language
Rocky基础-Shell脚本基础知识
Wargames NATAS (0-10) problem solving essay
RPC中实现提供者信息变化后通知消费者
1、 Midwey addition, deletion, modification and query
office回退版本,从2021到2019
Some mistakes that Xiaobai often makes (update from time to time, and promise not to make them again next time)