-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolExample.java
More file actions
32 lines (26 loc) · 1.02 KB
/
Copy pathThreadPoolExample.java
File metadata and controls
32 lines (26 loc) · 1.02 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
/**
* Day 26 - Executor Framework: Thread Pools
*/
import java.util.concurrent.*;
public class ThreadPoolExample {
public static void main(String[] args) throws InterruptedException {
System.out.println("=== Thread Pool Types ===\n");
System.out.println("--- FixedThreadPool (2 threads) ---");
ExecutorService fixed = Executors.newFixedThreadPool(2);
for (int i = 0; i < 4; i++) {
final int num = i;
fixed.execute(() -> System.out.println("Task " + num));
}
fixed.shutdown();
fixed.awaitTermination(2, TimeUnit.SECONDS);
System.out.println("\n--- CachedThreadPool ---");
ExecutorService cached = Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
final int num = i;
cached.execute(() -> System.out.println("Cached Task " + num));
}
cached.shutdown();
cached.awaitTermination(2, TimeUnit.SECONDS);
System.out.println("\nExecutor management complete");
}
}