-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchTest.java
More file actions
81 lines (70 loc) · 2.34 KB
/
CountDownLatchTest.java
File metadata and controls
81 lines (70 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package Thread.concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/*
* CountDownLatch:
* 同步多任务,将当前任务阻塞,直到其他任务完成才有机会执行
* */
public class CountDownLatchTest {
private static int size=2;
public static void main(String[] args) {
ExecutorService service=Executors.newCachedThreadPool();
CountDownLatch countDownLatch=new CountDownLatch(size);
for(int i=0;i<2;i++)
service.execute(new WaitThread(countDownLatch));
for(int i=0;i<size;i++)
service.execute(new TaskThread(countDownLatch));
try {
TimeUnit.MICROSECONDS.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class WaitThread implements Runnable{
private CountDownLatch countDownLatch;
//代表当前任务的编号
private static int count=0;
private static final int id=count++;
public WaitThread(CountDownLatch countDownLatch){
this.countDownLatch=countDownLatch;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"等待所有的TaskThread执行完毕。。。。。");
//执行时阻塞自己,直到所有的任务线程完成
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"第"+id+"个 等待线程执行完毕");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("WaitTHread interrupted");
}
}
}
class TaskThread implements Runnable{
private CountDownLatch countDownLatch;
private static int count=0;
private final int id=++count;
public TaskThread(CountDownLatch countDownLatch){
this.countDownLatch=countDownLatch;
}
public void run() {
doWork();
//每次减少1,减少到0时,就可以执行 等待线程了
countDownLatch.countDown();
}
//模拟任务线程执行任务
public void doWork(){
try {
System.out.println(Thread.currentThread().getName()+"第"+id+"个TaskThread正在执行。。。");
TimeUnit.MILLISECONDS.sleep(2000);
System.out.println(Thread.currentThread().getName()+"第"+id+"个任务线程执行完毕");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("TaskThread is interrupted");
}
}
}