-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorExample.java
More file actions
36 lines (29 loc) · 976 Bytes
/
Copy pathExecutorExample.java
File metadata and controls
36 lines (29 loc) · 976 Bytes
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
/**
* Day 26 - Executor Framework
*/
import java.util.concurrent.*;
public class ExecutorExample {
public static void main(String[] args) {
System.out.println("=== Executor Framework ===\n");
ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 5; i++) {
final int taskNum = i;
executor.execute(() -> {
System.out.println("Task " + taskNum + " running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Task " + taskNum + " completed");
});
}
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All tasks completed");
}
}