-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCancellationDemo.java
More file actions
60 lines (50 loc) · 2.16 KB
/
Copy pathCancellationDemo.java
File metadata and controls
60 lines (50 loc) · 2.16 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
package dev.karthik.concurrencylab;
import java.time.Duration;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
final class CancellationDemo {
private CancellationDemo() {
}
static void run() throws InterruptedException {
System.out.println("\n3) CANCELLATION — request, observe, stop safely");
try (var executor = Executors.newSingleThreadExecutor()) {
Future<?> order = executor.submit(CancellationDemo::processOrder);
Thread.sleep(Duration.ofMillis(650));
boolean requestAccepted = order.cancel(true);
System.out.printf(
" Caller requested cancellation (accepted=%s).%n",
requestAccepted);
try {
order.get();
} catch (CancellationException expected) {
System.out.println(
" Caller observed the expected cancelled outcome.");
} catch (java.util.concurrent.ExecutionException unexpected) {
throw new IllegalStateException(
"Order failed instead of cancelling",
unexpected.getCause());
}
}
}
private static void processOrder() {
for (int step = 1; step <= 10; step++) {
try {
// Sleep responds to Future.cancel(true) by throwing on interruption.
Thread.sleep(Duration.ofMillis(150));
} catch (InterruptedException cancelled) {
// Preserve the signal for any caller further up the stack.
Thread.currentThread().interrupt();
System.out.println(
" Worker observed the interrupt and stopped safely.");
return;
}
// CPU-bound loops should check this flag periodically themselves.
if (Thread.currentThread().isInterrupted()) {
System.out.println(" Worker observed cancellation in CPU work.");
return;
}
System.out.printf(" completed order step %d/10%n", step);
}
}
}