-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchTest1.java
More file actions
70 lines (62 loc) · 1.92 KB
/
Copy pathCountDownLatchTest1.java
File metadata and controls
70 lines (62 loc) · 1.92 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
package four;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchTest1 {
public static void main(String[] args) {
final CountDownLatch latch = new CountDownLatch(3);
System.out.println("主线程开始执行 ----- ");
ExecutorService es1 = Executors.newSingleThreadExecutor();
es1.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println("线程 : " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" 线程 " + Thread.currentThread().getName() + " 结束 ");
latch.countDown();
}
});
es1.shutdown();
ExecutorService es2 = Executors.newSingleThreadExecutor();
es2.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println("线程 : " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println(" 线程 " + Thread.currentThread().getName() + " 结束 ");
}
});
es2.shutdown();
ExecutorService es3 = Executors.newSingleThreadExecutor();
es3.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
System.out.println("线程 : " + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
System.out.println(" 线程 " + Thread.currentThread().getName() + " 结束 ");
}
});
es3.shutdown();
System.out.println("两个线程执行完毕============ ");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行结束============== ");
}
}