当前位置:网站首页>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 .
边栏推荐
- Current limiting and fusing of gateway gateway in Spirng cloud
- JS output uincode code
- Working principle of sentinel series (concept)
- @Detailed explanation of propertysource usage method and operation principle mechanism
- How to Algorithm Evaluation Methods
- 2021.9.29 learning log MIME type
- Jeffery0207 blog navigation
- Windbos common CMD (DOS) command set
- Cross compile HelloWorld with cmake
- Pyqt5 module
猜你喜欢

MySQL log management and master-slave replication

MySQL transactions and foreign keys

Vagrant virtual machine installation, disk expansion and LAN access tutorial

MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.

Solutions to conflicts between xampp and VMware port 443

20 flowable container (event sub process, things, sub process, pool and pool)

OpenGL馬賽克(八)

Qmessagebox in pyqt5

18 flowable task manualtask and receivetask

Mysql database crud operation
随机推荐
Tongweb crawl performance log script
MySQL installation, architecture and management
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
KVM virtualization management tool
Windbos common CMD (DOS) command set
2 first experience of drools
Integer tips
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
Mongodb Multi - field Aggregation group by
2020 personal annual summary
[China & some provinces and cities] JSON file for offline map visualization
Pychart professional edition's solution to SQL script error reporting
Automatic database backup (using Navicat)
Pyqt5 module
Etcd understanding of microservice architecture
Quartz database storage
Deleted the jupyter notebook in the jupyter interface by mistake
Service architecture diagram of Nacos series