当前位置:网站首页>Concurrent programming -- countdownlatch combined with thread pool
Concurrent programming -- countdownlatch combined with thread pool
2022-06-13 05:47:00 【Coffee is not bitter**】
In distributed projects , Realize relevant function calls through distributed architecture , This is inevitable . I'm in the project , For example, the display of the product details information page , Share scenarios such as graph composition , It will involve several sub module functions to obtain information .CountdownLatch It can well realize the function realization of similar scenarios .
CountdownLatch Basic introduction
It can make one thread wait for other threads to finish their work before executing .
details : Concurrent programming –CountdownLatch && CyclicBarrier
Scenario introduction
combination actor and course Information is combined into a sharing diagram , Process only , Core code emulation .
Code implementation
public class CountDownLatchService {
protected Actor actor;
protected Course course;
protected CountDownLatchService(Actor actor, Course course) {
this.actor = actor;
this.course = course;
}
private static final int threadSize = 5;
private boolean isRunning = false;
private static final CountDownLatch latch = new CountDownLatch(2);
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
threadSize + 1,
threadSize + 1,
10,
TimeUnit.SECONDS
, new SynchronousQueue<>());
public int init() {
if (isRunning == true) {
System.out.println(" The thread is running ...");
return -1;
}
isRunning = true;
executor.execute(() -> {
System.out.println(" obtain actor Related information ");
course.setName(" Drowsy courseName");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println("1:"+latch.getCount());
});
executor.execute(() -> {
System.out.println(" obtain course Of name");
actor.setName(" Drowsy actorName");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println("2:"+latch.getCount());
});
executor.execute(() -> {
System.out.println(" obtain course Of type");
// Just test the simulation , A real project may be a distributed related task invocation
course.setType(1L);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println("3:"+latch.getCount());
});
executor.execute(() -> {
System.out.println(" Wait for all information to return =");
try {
System.out.println("value:"+latch.getCount());
latch.await();
// Set up isRunning=false
isRunning = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("course info= " + course.getName());
System.out.println("actor info= " + actor.getName());
// Perform additional splicing synthesis logic
System.out.println(" Mosaic picture complete ");
});
executor.shutdown();
System.out.println(" Message return end ");
return 0;
}
public static void main(String[] args) {
Actor actor = new Actor();
Course course = new Course();
CountDownLatchService countDownLatchService = new CountDownLatchService(actor, course);
countDownLatchService.init();
}
}
Something to watch out for
CountDownLatch Initial values need to be carefully considered , Such as the above demo, I need three threads to get information to execute , Every time you execute latch.countDown(),count It will decrease 1. When count by 0 When , Will execute await.
What do you mean ?
For example, your initial value is 2, Three were executed latch.countDown();
For example, your initial value is 4, Three were executed latch.countDown();
The two results must be different , If you look at the source code, you will find , All calls await The thread of the method will be blocked in AQS In the blocking queue , By judgment count Is it 0 To decide whether to evoke .
边栏推荐
- Django uses redis to store sessions starting from 0
- Unity game optimization [Second Edition] learning record 6
- 2021.9.29学习日志-Restful架构
- Parallelgateway and exclusivegateway of 14 gateways
- How to Algorithm Evaluation Methods
- @Detailed explanation of propertysource usage method and operation principle mechanism
- One of PowerShell optimizations: prompt beautification
- Three paradigms of MySQL
- 11 signalthrowingevent and signalboundaryevent of flowable signal event
- Qmessagebox in pyqt5
猜你喜欢

Quartz database storage

OpenGL mosaic (VIII)

Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience

One of PowerShell optimizations: prompt beautification

MySQL installation, architecture and management

Django uses redis to store sessions starting from 0

为什么那么多人讨厌A-Spice

MySQL fuzzy query and sorting by matching degree

890. Find and Replace Pattern

KVM virtualization management tool
随机推荐
Working principle of sentinel series (concept)
2021-9-19
SQL table columns and statements of database
MySQL transactions and foreign keys
Randomly fetch data from the list
Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides
Automatic database backup (using Navicat)
About the solution of pychart that cannot be opened by double clicking
Quartz basic use
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Config server configuration center of Nacos series
Quartz database storage
Sentinel series integrates Nacos and realizes dynamic flow control
使用cmake交叉編譯helloworld
20 flowable container (event sub process, things, sub process, pool and pool)
Error: unmapped character encoding GBK
Fast power code
Basic application of sentinel series
Three paradigms of MySQL
【自动化测试】Cypress手册