-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorFramework.java
More file actions
145 lines (129 loc) · 5.55 KB
/
Copy pathExecutorFramework.java
File metadata and controls
145 lines (129 loc) · 5.55 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Phase7_Concurrency.Multithreading;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* The Executor Framework
* ----------------------
* Introduced in Java 5, the framework decouples WORK from the THREAD
* that runs it. The hierarchy:
* <p>
*
* Executor - execute(Runnable)
* |
* ExecutorService - submit, invokeAll, invokeAny, shutdown
* |
* ScheduledExecutorService - schedule(...), scheduleAtFixedRate, ...
* <p>
*
* Why use it
* ----------
* - Reuse threads (no spin-up cost per task).
* - Bounded resources (no thread bombs).
* - Structured result handling (Future/Callable).
* - Built-in shutdown semantics.
* <p>
*
* Common factories (Executors.*)
* ------------------------------
* newFixedThreadPool(n) - n workers, unbounded queue.
* newCachedThreadPool() - 0..∞ workers, SynchronousQueue, 60s reap.
* newSingleThreadExecutor() - guaranteed serial execution.
* newScheduledThreadPool(n) - delayed and periodic tasks.
* newVirtualThreadPerTaskExecutor() - Java 21, one VT per task.
* newWorkStealingPool(parallelism) - ForkJoinPool wrapper.
* <p>
*
* Java 19+ goodies
* ----------------
* - ExecutorService now implements AutoCloseable — use try-with-resources.
* - newVirtualThreadPerTaskExecutor — perfect for I/O-bound fan-out.
* <p>
*
* Key methods
* -----------
* execute(Runnable)
* submit(Runnable) -> Future<?>
* submit(Callable<V>) -> Future<V>
* submit(Runnable, V) -> Future<V>
* invokeAll(Collection) -> wait for all, return list of Futures
* invokeAny(Collection) -> wait for the first SUCCESS, cancel rest
* shutdown / shutdownNow / awaitTermination / isShutdown / isTerminated
*/
public class ExecutorFramework {
public static void main(String[] args) throws Exception {
section("1) newFixedThreadPool — submit + Future");
try (ExecutorService es = Executors.newFixedThreadPool(3)) {
Future<Integer> f = es.submit(() -> {
Thread.sleep(80);
return 21 * 2;
});
System.out.println("answer = " + f.get());
section("2) execute (fire-and-forget Runnable)");
es.execute(() -> System.out.println(" ran on " + Thread.currentThread().getName()));
es.execute(() -> System.out.println(" ran on " + Thread.currentThread().getName()));
Thread.sleep(100);
section("3) invokeAll — wait for ALL");
List<Callable<Integer>> jobs = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
final int n = i;
jobs.add(() -> { Thread.sleep(60); return n * n; });
}
long t0 = System.currentTimeMillis();
List<Future<Integer>> all = es.invokeAll(jobs);
int sum = 0; for (Future<Integer> r : all) sum += r.get();
System.out.println("sum of squares = " + sum + " in "
+ (System.currentTimeMillis() - t0) + " ms");
section("4) invokeAny — first successful wins, cancel rest");
String winner = es.invokeAny(List.of(
() -> { Thread.sleep(150); return "slow"; },
() -> { Thread.sleep(50); return "fast"; },
() -> { Thread.sleep(300); return "slower"; }
));
System.out.println("invokeAny winner = " + winner);
} // try-with-resources -> shutdown + awaitTermination
section("5) newCachedThreadPool — grows on demand");
try (ExecutorService cached = Executors.newCachedThreadPool()) {
for (int i = 0; i < 5; i++) {
final int id = i;
cached.execute(() -> { sleep(60); System.out.println(" cached " + id); });
}
}
section("6) newSingleThreadExecutor — strict serial");
try (ExecutorService one = Executors.newSingleThreadExecutor()) {
for (int i = 0; i < 3; i++) {
final int id = i;
one.execute(() -> { System.out.println(" serial " + id); sleep(30); });
}
}
section("7) newVirtualThreadPerTaskExecutor — Java 21");
try (ExecutorService vts = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<Integer>> fs = new ArrayList<>();
for (int i = 0; i < 20; i++) {
final int id = i;
fs.add(vts.submit(() -> { Thread.sleep(50); return id; }));
}
int total = 0;
for (Future<Integer> f : fs) total += f.get();
System.out.println("VT sum = " + total + " (20 cheap virtual threads)");
}
section("8) Manual shutdown semantics");
ExecutorService es = Executors.newFixedThreadPool(1);
es.execute(() -> sleep(80));
es.execute(() -> sleep(80));
es.shutdown(); // accept no more
System.out.println("isShutdown=" + es.isShutdown());
if (!es.awaitTermination(2, TimeUnit.SECONDS)) {
es.shutdownNow(); // try to interrupt
}
System.out.println("isTerminated=" + es.isTerminated());
section("done");
}
private static void sleep(long ms) {
try { Thread.sleep(ms); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); }
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}