1
1
/*
2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
3
*
4
- * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
4
+ * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
5
5
*
6
6
* The contents of this file are subject to the terms of either the GNU
7
7
* General Public License Version 2 only ("GPL") or the Common Development
81
81
* @author Marek Potociar (marek.potociar at oracle.com)
82
82
* @since 2.18
83
83
*/
84
- // TODO implement AutoCloseable once switched to Java SE 7+
85
- public abstract class AbstractThreadPoolProvider <E extends ThreadPoolExecutor > {
84
+ public abstract class AbstractThreadPoolProvider <E extends ThreadPoolExecutor > implements AutoCloseable {
86
85
87
86
private static final ExtendedLogger LOGGER = new ExtendedLogger (
88
87
Logger .getLogger (AbstractThreadPoolProvider .class .getName ()), Level .FINEST );
@@ -95,13 +94,7 @@ public abstract class AbstractThreadPoolProvider<E extends ThreadPoolExecutor> {
95
94
private final String name ;
96
95
private final AtomicBoolean closed = new AtomicBoolean (false );
97
96
private final LazyValue <E > lazyExecutorServiceProvider =
98
- Values .lazy (new Value <E >() {
99
-
100
- @ Override
101
- public E get () {
102
- return createExecutor (getCorePoolSize (), createThreadFactory (), getRejectedExecutionHandler ());
103
- }
104
- });
97
+ Values .lazy ((Value <E >) () -> createExecutor (getCorePoolSize (), createThreadFactory (), getRejectedExecutionHandler ()));
105
98
106
99
/**
107
100
* Inheritance constructor.
@@ -211,11 +204,8 @@ protected int getCorePoolSize() {
211
204
* @see #createExecutor
212
205
*/
213
206
protected RejectedExecutionHandler getRejectedExecutionHandler () {
214
- return new RejectedExecutionHandler () {
215
- @ Override
216
- public void rejectedExecution (final Runnable r , final ThreadPoolExecutor executor ) {
217
- // TODO: implement the rejected execution handler method.
218
- }
207
+ return (r , executor ) -> {
208
+ // TODO: implement the rejected execution handler method.
219
209
};
220
210
}
221
211
@@ -345,51 +335,47 @@ private static PrivilegedAction<?> shutdownExecutor(
345
335
final int terminationTimeout ,
346
336
final TimeUnit terminationTimeUnit ) {
347
337
348
- return new PrivilegedAction <Void >() {
349
-
350
- @ Override
351
- public Void run () {
352
- if (!executorService .isShutdown ()) {
353
- executorService .shutdown ();
354
- }
355
- if (executorService .isTerminated ()) {
356
- return null ;
357
- }
338
+ return (PrivilegedAction <Void >) () -> {
339
+ if (!executorService .isShutdown ()) {
340
+ executorService .shutdown ();
341
+ }
342
+ if (executorService .isTerminated ()) {
343
+ return null ;
344
+ }
358
345
359
- boolean terminated = false ;
360
- boolean interrupted = false ;
361
- try {
362
- terminated = executorService .awaitTermination (terminationTimeout , terminationTimeUnit );
363
- } catch (InterruptedException e ) {
364
- if (LOGGER .isDebugLoggable ()) {
365
- LOGGER .log (LOGGER .getDebugLevel (),
366
- "Interrupted while waiting for thread pool executor " + executorName + " to shutdown." , e );
367
- }
368
- interrupted = true ;
346
+ boolean terminated = false ;
347
+ boolean interrupted = false ;
348
+ try {
349
+ terminated = executorService .awaitTermination (terminationTimeout , terminationTimeUnit );
350
+ } catch (InterruptedException e ) {
351
+ if (LOGGER .isDebugLoggable ()) {
352
+ LOGGER .log (LOGGER .getDebugLevel (),
353
+ "Interrupted while waiting for thread pool executor " + executorName + " to shutdown." , e );
369
354
}
355
+ interrupted = true ;
356
+ }
370
357
371
- try {
372
- if (!terminated ) {
373
- final List <Runnable > cancelledTasks = executorService .shutdownNow ();
374
- for (Runnable cancelledTask : cancelledTasks ) {
375
- if (cancelledTask instanceof Future ) {
376
- ((Future ) cancelledTask ).cancel (true );
377
- }
378
- }
379
-
380
- if (LOGGER .isDebugLoggable ()) {
381
- LOGGER .debugLog ("Thread pool executor {0} forced-shut down. List of cancelled tasks: {1}" ,
382
- executorName , cancelledTasks );
358
+ try {
359
+ if (!terminated ) {
360
+ final List <Runnable > cancelledTasks = executorService .shutdownNow ();
361
+ for (Runnable cancelledTask : cancelledTasks ) {
362
+ if (cancelledTask instanceof Future ) {
363
+ ((Future ) cancelledTask ).cancel (true );
383
364
}
384
365
}
385
- } finally {
386
- if (interrupted ) {
387
- // restoring the interrupt flag
388
- Thread . currentThread (). interrupt ( );
366
+
367
+ if (LOGGER . isDebugLoggable () ) {
368
+ LOGGER . debugLog ( "Thread pool executor {0} forced-shut down. List of cancelled tasks: {1}" ,
369
+ executorName , cancelledTasks );
389
370
}
390
371
}
391
- return null ;
372
+ } finally {
373
+ if (interrupted ) {
374
+ // restoring the interrupt flag
375
+ Thread .currentThread ().interrupt ();
376
+ }
392
377
}
378
+ return null ;
393
379
};
394
380
}
395
381
}
0 commit comments