当前位置:网站首页>Usage of countdownlatch
Usage of countdownlatch
2022-07-23 11:06:00 【Road of Kings 001】
Try to solve a problem today , Is in the Android Realize multithread synchronization in , But it requires several variables to be synchronized at the same time .
at that time , The first thing to think about is , It must be used Android or JAVA Its own mechanism .
I found a man named CountDownLatch The mechanism of .
CountDownLatch Multithreaded synchronization class , It can coordinate the synchronization between multiple threads .
CountDownLatch The typical usage of is as follows :
1) Create a CountDownLatch example , And determine the number of threads or tasks that need to wait ;
2) A thread or task completes ,CountDownLatch Counter minus one ;
3) Use CountDownLatch.await(), etc. CountDownLatch The counter of is 0, Then run .
CountDownLatch There are three important methods in the class
// Constructors , Set the number of reference counts
public CountDownLatch(int count){}
// call await() Method will be suspended , It will wait until count The value is 0 To continue
public void await() throws InterruptedException { };
// and await() similar , Just wait for a certain time count Value hasn't changed to 0 It will continue
public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };
// take count Value reduction 1
public void countDown() { };Using this method , Sure enough, the problem was solved .
Note appended :
CountDownLatch and CyclicBarrier The difference between :
1)CountDownLatch It's a counter , Counter decrement , Until used up ; You must reconstruct or assign values if you want to use ;
2)CyclicBarrier The counter is like a valve , Until all threads arrive , Can be done reset And use it many times .
边栏推荐
- Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
- 讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
- 比特,比特,字節,字的概念與區別
- C ivalueconverter interface usage example
- 一次 MySQL 误操作导致的事故,「高可用」都不好使了
- Activiti工作流使用之Activiti-app的安装及流程创建
- 联合主键和索引
- Web server failed to start. Port 8080 was already in use.
- Dynamic memory management
- Concepts et différences de bits, bits, octets et mots
猜你喜欢

With only 5000 lines of code, AI renders 100 million landscape paintings on v853

Error in na.fail. default(list(Purchase = c(“CH“, “CH“, “CH“, “MM“, “CH“, : missing values in obj

C ivalueconverter interface usage example

"The six programming languages I want most!"
![[visual slam] orb slam: tracking and mapping recognizable features](/img/bb/d6a99bb1bff6bbe1f781e567cc1176.png)
[visual slam] orb slam: tracking and mapping recognizable features

web调用接口上传图片到七牛云

7、纹理映射

A case study on the collaborative management of medical enterprise suppliers, hospitals, patients and other parties by building a low code platform

9. Ray tracing

Compare the advantages and disadvantages of RDB and AOF modes of redis
随机推荐
【达人专栏】还不会用Apache Dolphinscheduler吗,大佬用时一个月写出的最全入门教学【二】
C1--Vivado配置VS Code文本编辑器环境2022-07-21
Redis source code and design analysis -- 11. Hash object
Data Lake: introduction to delta Lake
52832Dongle的安装
Redis source code and design analysis -- 12. Collection objects
SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION
数据库进程卡住解决
Cadence (IX) 17.4 rules and spacing settings
Alibaba cloud object storage service OSS front and rear joint debugging
9. Ray tracing
Redis source code and design analysis -- 9. String object
[Social Media Marketing] new idea of going to sea: WhatsApp business replaces Facebook
Concepts et différences de bits, bits, octets et mots
Analyse du code source et de la conception de redis - - 7. Liste rapide
7、纹理映射
A case study on the collaborative management of medical enterprise suppliers, hospitals, patients and other parties by building a low code platform
使用cmd安装pygame
安装企业版pycharm以及anaconda
Activiti工作流使用之新建bpmn文件