First, the application scenarios
In real life do have many such scenes: the need to wait for A, B, C, D before making F, E completed, A, B, C, D, E can be complicated to complete, in no particular order, for example: Weekend eat at home, there are three things to do, Dad cooks, mom cooking, son clear the table, put the dishes. We can see this scene is characterized by:
1. there before dinner N thing to do, to eat after everything done, things to be treated is N, each done a thing to be processed is reduced 1, when the matter is pending 0, you can eat.
2. Everything can be processed in parallel, in no particular order, thus improving efficiency.
Around dinner, the whole process is as follows:
二、Show me code
Category code structured as follows:
I、EatingActivity.java
class EatingActivity implements Runnable {
private CountDownLatch countDownLatch;
public EatingActivity(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
try {
//等待吃饭
System.out.println("Waiting for dinner...");
this.countDownLatch.await();
//所有事情做完后,await被唤醒,开始吃饭
System.out.println("Start eating...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
复制代码
II MakeRice.java
/**
* 做饭
*/
class MakeRice implements Runnable {
private CountDownLatch countDownLatch;
public MakeRice(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
exec();
}
/**
* 模拟做饭
*/
private void exec() {
try {
System.out.println("Start making rice...");
long millis = ((int)(1+Math.random()*(5-1+1))) * 1000;
Thread.sleep(millis);
System.out.println("Making rice is finished.");
//待处理事情减1
this.countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
复制代码
III、MakeDish.java
/**
* 做菜
*/
class MakeDish implements Runnable {
private CountDownLatch countDownLatch;
public MakeDish(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
exec();
}
/**
* 模拟做菜
*/
private void exec() {
try {
System.out.println("Start making dish...");
long millis = ((int)(1+Math.random()*(5-1+1))) * 1000;
Thread.sleep(millis);
System.out.println("Making dish is finished.");
//待处理事情减1
this.countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
复制代码
IV、CleanUpTable.java
/**
* 收拾桌子
*/
class CleanUpTable implements Runnable {
private CountDownLatch countDownLatch;
public CleanUpTable(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
exec();
}
/**
* 模拟收拾桌子
*/
private void exec() {
try {
System.out.println("Start making rice...");
long millis = ((int)(1+Math.random()*(5-1+1))) * 1000;
Thread.sleep(millis);
System.out.println("Cleaning up table is finished.");
//待处理事情减1
this.countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
复制代码
V、CountDownLatchTest.java
/**
* @ClassName CountDownLatchTest
* @Description 验证CountDownLatch
* @Author 铿然一叶
* @Date 2019/10/7 22:32
* @Version 1.0
* javashizhan.com
**/
public class CountDownLatchTest {
public static void main(String[] args) {
//为了吃饭,有3件事情要做
CountDownLatch countDownLatch = new CountDownLatch(3);
//吃饭活动
Thread eatingActivity = new Thread(new EatingActivity(countDownLatch));
eatingActivity.start();
//做饭
Thread makeRice = new Thread(new MakeRice(countDownLatch));
//做菜
Thread makeDish = new Thread(new MakeDish(countDownLatch));
//收拾桌子
Thread cleanUpTable = new Thread(new CleanUpTable(countDownLatch));
//并行开始做事情
makeRice.start();
makeDish.start();
cleanUpTable.start();
}
}
复制代码
Output log:
Waiting for dinner...
Start making rice...
Start making rice...
Start making dish...
Cleaning up table is finished.
Making rice is finished.
Making dish is finished.
Start eating...
复制代码
Third, other scenarios - fight Mission
Mission to fight scenes, how many people can come after the full group, used the counter, looks can be achieved with CountDownLatch, in fact, not necessary, because you can not fight groups in parallel, as long as there are counters can be achieved.
IV Summary
CountDownLatch of application scenarios:
1. few things to begin after the completion of another thing.
2. The need to do several things can be done independently, and can be processed in parallel.
end.
Java Concurrency (a) knowledge map
Java Concurrency (II) atom of
concurrent programming in Java (c) the visibility of
Java Concurrency (d) ordering of
concurrent programming in Java (e) creates a thread way Overview
Getting Started with Java concurrent programming (six) synchronized usage of
Java concurrent programming entry (seven) easy to understand and use wait and notify scene
Getting concurrent programming Java (eight) thread lifecycle
Java concurrent programming entry (nine) deadlocks and deadlock locate
Java concurrency entry (X) lock to optimize
Java Getting concurrent programming (XI) limiting the flow restrictor Spring scene and realize
Getting Started with Java concurrent programming (xii) the producer and consumer model - code templates
Java concurrency entry (13) read-write locks and cache template
Site: javashizhan.com/
Micro-channel public number: