-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationForTasks.java
311 lines (284 loc) · 10.7 KB
/
ApplicationForTasks.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package kp.tasks;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import kp.utils.Printer;
import kp.utils.Utils;
/**
* The main class for the task research.
* <p>
* Researched classes:
* </p>
* <ul>
* <li>{@link Runnable}
* <li>{@link Callable}
* <li>{@link Future}
* <li>{@link CompletableFuture}
* <li>{@link FutureTask}
* <li>{@link ExecutorService}
* <li>{@link ForkJoinPool}
* <li>{@link ThreadFactory}
* </ul>
*/
public class ApplicationForTasks {
/**
* The constructor.
*/
public ApplicationForTasks() {
super();
}
/**
* The entry point for the application.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
final ApplicationForTasks application = new ApplicationForTasks();
Printer.printHor();
application.compareExecutors();
application.completeCompletableFutures();
application.executeFutureTasks();
application.executeRunnablesAndCallables();
application.invokeCallableTasksCollection();
final SafeFutureCompletion safeFutureCompletion = new SafeFutureCompletion();
safeFutureCompletion.process();
final StageCompletion stageCompletion = new StageCompletion();
stageCompletion.completeSingleStageWithDependencies();
stageCompletion.completeEitherOfTwoStages();
stageCompletion.completeBothOfTwoStages();
VirtualThreadsAndPlatformThreads.startPlatformThreadsAndVirtualThreads();
VirtualThreadsAndPlatformThreads.executeTasksWithExecutorService();
}
/**
* Compares the {@link ExecutorService}s.
*
*/
private void compareExecutors() {
final Map<String, ExecutorService> poolMap = new TreeMap<>();
/*-
* For fixed size thread pool it sets up a thread pool with one thread per core
* which is appropriate for CPU bound tasks
*/
poolMap.put("A", Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
/*-
* unbounded thread pool, with automatic thread reclamation
*/
poolMap.put("B", Executors.newCachedThreadPool());
/*-
* constructor taken from 'newSingleThreadExecutor()' code
*/
poolMap.put("C", new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()));
/*-
*single background thread
*/
poolMap.put("D", Executors.newSingleThreadExecutor());
poolMap.put("E", Executors.newWorkStealingPool());
poolMap.put("F", new ForkJoinPool());
final Consumer<Entry<String, ExecutorService>> keyConsumer = entry -> {
final ExecutorService pool = entry.getValue();
final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "supplied value", pool);
final String result;
try {
result = future.get();
} catch (InterruptedException e) {
pool.shutdownNow();
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
return;
} catch (ExecutionException e) {
pool.shutdownNow();
Printer.printExecutionException(e);
throw new IllegalStateException(e);
}
shutdownAndAwaitTermination(pool);
Printer.printf("Pool %s: future result[%s], pool class[%s]", entry.getKey(), result,
pool.getClass().getSimpleName());
};
poolMap.entrySet().forEach(keyConsumer);
Printer.printHor();
}
/**
* Shutdowns pool and awaits termination.<br>
* The way of pool shutdown recommended by Oracle.
*
* @param pool the pool
*/
private void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
if (!pool.awaitTermination(1, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
if (!pool.awaitTermination(1, TimeUnit.SECONDS)) {
Printer.print("Pool did not terminate");
}
}
} catch (InterruptedException e) {
pool.shutdownNow();
Thread.currentThread().interrupt();// Preserve interrupt status
throw new IllegalStateException(e);
}
}
/**
* Completes the {@link CompletableFuture}s.
*
*/
private void completeCompletableFutures() {
final CompletableFuture<String> completableFuture1 = CompletableFuture//
.supplyAsync(() -> "supplied value 1");
final CompletableFuture<String> completableFuture2 = new CompletableFuture<String>()//
.completeAsync(() -> "supplied value 2");
final CompletableFuture<String> completableFuture3 = new CompletableFuture<String>().newIncompleteFuture();
completableFuture3.complete("completing value 3");
// If you already know the result of a computation
final Future<String> completableFuture4 = CompletableFuture.completedFuture("completing value 4");
Printer.printf("Completable futures are completed: 1[%B], 2[%B], 3[%B], 4[%B]", completableFuture1.isDone(),
completableFuture2.isDone(), completableFuture3.isDone(), completableFuture4.isDone());
try {
Printer.printf("Completable futures results: 1[%s], 2[%s], 3[%s], 4[%s]", completableFuture1.get(),
completableFuture2.get(), completableFuture3.get(), completableFuture4.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
System.exit(1);
} catch (ExecutionException e) {
Printer.printExecutionException(e);
System.exit(1);
}
Printer.printHor();
}
/**
* Executes the {@link FutureTask}s with a {@link Runnable} or a
* {@link Callable}.
*
*/
private void executeFutureTasks() {
// The FutureTask wraps a Callable object.
final FutureTask<String> futureTaskCallable = new FutureTask<>(() -> "callable result");
// The FutureTask wraps a Runnable object.
final FutureTask<String> futureTaskRunnable = new FutureTask<>(() -> Printer.print("«runnable action»"),
"successful completion result");
try (final ExecutorService pool = Executors.newFixedThreadPool(10)) {
pool.execute(futureTaskCallable);
pool.execute(futureTaskRunnable);
Printer.printf("Before sleep - future task is completed: R[%5S], C[%5S]", futureTaskRunnable.isDone(),
futureTaskCallable.isDone());
Utils.sleepMillis(10);
Printer.printf("After sleep - future task is completed: R[%5S], C[%5S]", futureTaskRunnable.isDone(),
futureTaskCallable.isDone());
try {
Printer.printf("Future task result: C[%s]", futureTaskCallable.get());
Printer.printf("Future task result: R[%s]", futureTaskRunnable.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
System.exit(1);
} catch (ExecutionException e) {
Printer.printExecutionException(e);
System.exit(1);
}
}
Printer.printHor();
}
/**
* Executes various {@link Runnable}s and {@link Callable}s.
*
*/
private void executeRunnablesAndCallables() {
final StringBuilder strBld = new StringBuilder();
final Thread thread = new Thread(() -> strBld.append("«action 1»"));
final ThreadFactory threadFactory = Executors.defaultThreadFactory();
final Thread threadFromFactory = threadFactory.newThread(() -> strBld.append("«action 2»"));
Printer.printf("Thread names: thread[%s], threadFromFactory[%s]", thread.getName(),
threadFromFactory.getName());
Printer.print("Runnable actions:");
Printer.print("(with '▌▐' are printed Java code line markers)");
strBld.append("\n▐A▌");
thread.start();
Utils.sleepMillis(10);
strBld.append("▐B▌");
threadFromFactory.start();
try (final ExecutorService pool = Executors.newFixedThreadPool(10)) {
pool.execute(() -> strBld.append("«action 3»"));
strBld.append("▐C▌");
CompletableFuture.runAsync(() -> strBld.append("«action 4»"), pool);
strBld.append("▐D▌");
/*- 3 alternatives of using the 'pool.submit(...)' */
final Future<?> taskFutureRun = pool.submit(() -> {
strBld.append("«action 5»");
return "runnable task result (R)";
});
strBld.append("▐E▌");
final Runnable runnable = () -> strBld.append("«action 6»");
strBld.append("▐F▌");
final Future<?> taskFutureCalFromRun = pool
.submit(Executors.callable(runnable, "callable task result (R►C)"));
final Future<?> taskFutureCal = pool.submit(() -> "callable task result (C)");
strBld.append("▐G▌\n");
Printer.printObject(strBld);
Printer.printf("Future task is completed: 'runnable'[%5S], 'runnable►callable'[%5S], 'callable'[%5S]",
taskFutureRun.isDone(), taskFutureCalFromRun.isDone(), taskFutureCal.isDone());
try {
// Future from runnable returns null if the task has finished correctly.
Printer.print("Future results:");
Printer.printf("\t'runnable'[%s]", taskFutureRun.get());
Printer.printf("\t'runnable►callable'[%s]", taskFutureCalFromRun.get());
Printer.printf("\t'callable'[%s]", taskFutureCal.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
System.exit(1);
} catch (ExecutionException e) {
Printer.printExecutionException(e);
System.exit(1);
}
}
Printer.printHor();
}
/**
* Invokes the {@link Callable} tasks collection using the method
* {@link ExecutorService#invokeAll}.
*
*/
private void invokeCallableTasksCollection() {
final List<Callable<String>> callableList = List.of(//
() -> "from callable task A", () -> "from callable task B");
final Consumer<Future<String>> futureConsumer = future -> {
try {
Printer.printf("Consumed future result[%s]", future.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
System.exit(1);
} catch (ExecutionException e) {
Printer.printExecutionException(e);
System.exit(1);
}
};
try (final ExecutorService pool = Executors.newFixedThreadPool(10)) {
List<Future<String>> futureList = null;
try {
futureList = pool.invokeAll(callableList);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Printer.printInterruptedException(e);
System.exit(1);
}
futureList.forEach(futureConsumer);
}
Printer.printHor();
}
}