当前位置:网站首页>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();
}
}
}
边栏推荐
- Configuration of uni app page.json title bar
- Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao
- JS string interception
- Clear up some disk space and try again
- Treap
- 3、 Midway interface security certification
- [Shangshui Shuo series] final rad New Literacies
- Kotlin learning note 1 - variables, functions
- How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model
- Online lover
猜你喜欢

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao

RPC中实现提供者信息变化后通知消费者

Digital collections are both outlets and risks!

利用opencv 做一个简单的人脸识别

Route interceptor

3587. Connected graph (Jilin University postgraduate entrance examination machine test question)

Confusing defer, return, named return value, anonymous return value in golang

Unity solves the package manager "you see to be offline"

Why does the metauniverse need NFT?

Take out the string in brackets
随机推荐
Selenium webdriver page refresh
Configuration of uni app page.json title bar
Super complete summary: how to operate files in go language
Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
【情感】何为“优秀”
mysql URL
Sed add content after a line
Interviewer: man, how much do you know about the read-write lock of go language?
Houdini notes
Grpc learning notes
安装软件时提示【An error occurred while trying to create a file in the destination directory: 拒绝访问】的解决方法
读写锁、共享锁、独占锁
SQL learning
2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway
面试官:哥们Go语言的读写锁了解多少?
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
First acquaintance with JVM
Wargames NATAS (16-19) problem solving essays
Four data interaction modes of go grpc
Precautions for using kettle excel input