-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedPoolInterruptedSleep.java
187 lines (149 loc) · 5.11 KB
/
FixedPoolInterruptedSleep.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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public final class FixedPoolInterruptedSleep implements Runnable {
/**
* Thread sleeps for the given Time.
*/
public static class RunnerSleep implements Runnable {
private final int mCount;
private final long mTimeOutMs;
/**
* Thread sleeps for the given Time.
*
* @param pCount Thread Counter for display
* @param pTimeOutMs Run Time in Milliseconds
*/
public RunnerSleep(final int pCount, final long pTimeOutMs) {
super();
mCount = pCount;
mTimeOutMs = pTimeOutMs;
}
@Override
public void run() {
try {
Thread.sleep(mTimeOutMs);
} catch (final InterruptedException e) {
helperPrintStats("RunnerSleep " + mCount, "INTR");
return;
}
helperPrintStats("RunnerSleep " + mCount + " fin", "");
}
}
/**
* Thread for Shutdown Hook.
*/
public static class ShutdownHookThread extends Thread {
private final Thread mMainThread;
public ShutdownHookThread(final Thread pMainThread) {
super();
mMainThread = pMainThread;
}
@Override
public void run() {
helperPrintStats("ShutdownHookThread", "START");
mMainThread.interrupt();
try {
// Wait until Main Thread stops
mMainThread.join(TimeUnit.SECONDS.toMillis(10));
} catch (final InterruptedException e1) {
// Ignore
}
helperPrintStats("ShutdownHookThread", "FIN");
}
}
/**
* Interrupter Thread.
*/
public static class ThreadInterupter extends Thread {
private final Thread mMainThread;
private final long mTimeOutMs;
/**
* Interrupter Thread. Interrupt passed Thread after Time Out.
*
* @param pMainThread Thread to Interrupt
* @param pTimeOutMs Time Out before Interrupter strikes
*/
public ThreadInterupter(final Thread pMainThread, final long pTimeOutMs) {
super();
mMainThread = pMainThread;
mTimeOutMs = pTimeOutMs;
}
@Override
public void run() {
super.run();
try {
Thread.sleep(mTimeOutMs);
mMainThread.interrupt();
helperPrintStats("ThreadInterupter", "INTR");
} catch (final InterruptedException e) {
// Ignore
}
}
}
/**
* Start Time of Application.
*/
public static final long START_TIME = System.currentTimeMillis();
/**
* Print Runtime, Thread ID, Thread Status, Major Event, Thread Count.
*
* @param pThreadStatus Status Message of Thread
* @param pEvent Event like 'INTR'
*/
public static void helperPrintStats(final String pThreadStatus, final String pEvent) {
System.out.format("%,7d ms %5d ThrID %-25s %15s %5d #Threads\n",
System.currentTimeMillis() - START_TIME,
Thread.currentThread().getId(),
pThreadStatus,
pEvent,
Thread.activeCount());
}
/**
* Main Method. Just start Main Thread.
*
* @param args Nothing to pass
*/
public static void main(final String[] args) {
new Thread(new FixedPoolInterruptedSleep()).start();
}
@Override
public void run() {
// Register Shutdown Hook
final Thread tHookThread = new ShutdownHookThread(Thread.currentThread());
Runtime.getRuntime().addShutdownHook(tHookThread);
// Interrupter Thread will interrupt after 15 Seconds
new ThreadInterupter(Thread.currentThread(), 15000).start();
// Executor
final ExecutorService ex = Executors.newFixedThreadPool(2);
for (int i = 0; i < 1000; i++) {
final Runnable runner = new RunnerSleep(i, 1000);
ex.submit(runner);
helperPrintStats("main " + i + " submitted", "");
// Is Thread interrupted break Submittion
if (Thread.currentThread().isInterrupted()) {
helperPrintStats("main " + i, "BREAK SUBMITTED");
break;
}
}
helperPrintStats("main", "fin submitted");
// Friendly Executor Shutdown
ex.shutdown();
try {
// Wait for Executor to Shutdown
ex.awaitTermination(1, TimeUnit.HOURS);
} catch (final InterruptedException e) {
helperPrintStats("main", "INTR AWAIT");
}
// Force Executor Shutdown
helperPrintStats("main", "shutdown now");
ex.shutdownNow();
// Wait until Executor really finishes
try {
ex.awaitTermination(1, TimeUnit.HOURS);
} catch (final InterruptedException e) {
// Ignore
}
helperPrintStats("main fin", "");
}
}