当前位置:网站首页>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 .
边栏推荐
- About the solution of pychart that cannot be opened by double clicking
- 2020 personal annual summary
- Unity游戏优化(第2版)学习记录7
- Misunderstanding of tongweb due to ease of use
- 19 calling subprocess (callactivity) of a flowable task
- 设置自定义dialog的正确宽高
- MySQL basic query
- @Detailed explanation of propertysource usage method and operation principle mechanism
- redis
- Service architecture diagram of Nacos series
猜你喜欢
3. Postman easy to use
MongoDB 多字段聚合Group by
Mysql database crud operation
Sentinel series introduction to service flow restriction
Three paradigms of MySQL
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
ZABBIX wechat alarm
MongoDB 多字段聚合Group by
12 error end event and terminateendevent of end event
Why do so many people hate a-spice
随机推荐
Top slide immersive dialog
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
Why do so many people hate a-spice
2021.9.30学习日志-postman
One of PowerShell optimizations: prompt beautification
Initial redis experience
Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
Current limiting and fusing of gateway gateway in Spirng cloud
High availability of Nacos series
Quartz basic use
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
Application virtual directory static resource configuration on tongweb
Tongweb adapts to openrasp
Detailed explanation of R language sparse matrix
Pyqt5 module
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
MySQL main query and sub query
A fast week
MySQL installation in Linux Environment
2 first experience of drools