-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinFixedPoolInterruptedSleep.java
64 lines (51 loc) · 1.84 KB
/
MinFixedPoolInterruptedSleep.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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public final class MinFixedPoolInterruptedSleep implements Runnable {
public static void main(final String[] args) {
new Thread(new MinFixedPoolInterruptedSleep()).start();
}
@Override
public void run() {
// Register Shutdown Hook
final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
mainThread.interrupt();
mainThread.join(10000); // Chance for Main Thread to close
} catch (final InterruptedException e) {
// Ignore
}
System.out.println("SHUTDOWNHOOK FORCED EXIT");
}));
final ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 1E4; i++) {
executor.submit(() -> {
try {
Thread.sleep(1000);
System.out.println("sub tread ran");
} catch (final InterruptedException e) {
System.out.println("SUB THREAD INTERRUPTED");
}
});
if (Thread.currentThread().isInterrupted()) {
System.out.println("MAIN SUBMIT INTERRUPTED");
break;
}
}
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.HOURS);
} catch (final InterruptedException e) {
// Ignore
}
System.out.println("main force shutdown");
executor.shutdownNow();
try {
executor.awaitTermination(1, TimeUnit.HOURS);
} catch (final InterruptedException e) {
// Ignore
}
System.out.println("main finished");
}
}