Skip to content

Commit 6545d16

Browse files
committed
Added shutdown() for ExecutorService
1 parent 5022896 commit 6545d16

6 files changed

Lines changed: 16 additions & 18 deletions

concurrent/CaptureUncaughtException.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6-
import onjava.TimedAbort;
76

87
class ExceptionThread2 implements Runnable {
98
@Override
@@ -40,21 +39,17 @@ public Thread newThread(Runnable r) {
4039

4140
public class CaptureUncaughtException {
4241
public static void main(String[] args) {
43-
new TimedAbort(4);
4442
ExecutorService exec = Executors.newCachedThreadPool(
4543
new HandlerThreadFactory());
4644
exec.execute(new ExceptionThread2());
45+
exec.shutdown();
4746
}
4847
}
4948
/* Output:
50-
HandlerThreadFactory@14991ad creating new Thread
51-
created Thread[Thread-1,5,main]
52-
eh = MyUncaughtExceptionHandler@d93b30
53-
run() by Thread-1
54-
eh = MyUncaughtExceptionHandler@d93b30
55-
HandlerThreadFactory@14991ad creating new Thread
56-
created Thread[Thread-2,5,main]
57-
eh = MyUncaughtExceptionHandler@1351c58
49+
HandlerThreadFactory@4e25154f creating new Thread
50+
created Thread[Thread-0,5,main]
51+
eh = MyUncaughtExceptionHandler@70dea4e
52+
run() by Thread-0
53+
eh = MyUncaughtExceptionHandler@70dea4e
5854
caught java.lang.RuntimeException
59-
TimedAbort 4
6055
*/

concurrent/ExceptionThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public void run() {
1414
public static void main(String[] args) {
1515
ExecutorService es = Executors.newCachedThreadPool();
1616
es.execute(new ExceptionThread());
17+
es.shutdown();
1718
}
1819
}

concurrent/NaiveExceptionHandling.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
public class NaiveExceptionHandling {
1010
public static void main(String[] args) {
11+
ExecutorService es =
12+
Executors.newCachedThreadPool();
1113
try {
12-
ExecutorService exec =
13-
Executors.newCachedThreadPool();
14-
exec.execute(new ExceptionThread());
14+
es.execute(new ExceptionThread());
1515
} catch(RuntimeException ue) {
1616
// This statement will NOT execute!
1717
System.out.println("Exception was handled!");
18+
} finally {
19+
es.shutdown();
1820
}
1921
}
2022
}

concurrent/SettingDefaultHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6-
import onjava.TimedAbort;
76

87
public class SettingDefaultHandler {
98
public static void main(String[] args) {
10-
new TimedAbort(4);
119
Thread.setDefaultUncaughtExceptionHandler(
1210
new MyUncaughtExceptionHandler());
1311
ExecutorService es = Executors.newCachedThreadPool();
1412
es.execute(new ExceptionThread());
13+
es.shutdown();
1514
}
1615
}
1716
/* Output:
1817
caught java.lang.RuntimeException
19-
TimedAbort 4
2018
*/

concurrent/SwallowedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public static void main(String[] args)
1212
exec.submit(() -> {
1313
throw new RuntimeException();
1414
});
15-
exec.shutdownNow();
15+
exec.shutdown();
1616
}
1717
}

concurrent/ThreadSize.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static void main(String[] args) {
2929
System.out.println(
3030
e.getClass().getSimpleName() + ": " + count);
3131
System.exit(0);
32+
} finally {
33+
exec.shutdown();
3234
}
3335
}
3436
}

0 commit comments

Comments
 (0)