2
2
3
3
import java .time .Duration ;
4
4
import java .util .List ;
5
- import java .util .Optional ;
6
5
import java .util .concurrent .Executor ;
7
6
import java .util .concurrent .ScheduledExecutorService ;
8
7
import java .util .concurrent .ThreadFactory ;
@@ -77,31 +76,30 @@ private static Runnable createShutdownTask(ThreadPoolConfig threadPoolConfig, En
77
76
@ Override
78
77
public void run () {
79
78
executor .shutdown ();
80
- final Duration shutdownTimeout = threadPoolConfig .shutdownTimeout ();
81
- final Optional <Duration > optionalInterval = threadPoolConfig .shutdownCheckInterval ();
82
- final long shutdownRemaining = shutdownTimeout .toNanos ();
83
- long remaining = shutdownRemaining ;
84
- final long interval = optionalInterval .orElse (Duration .ofNanos (Long .MAX_VALUE )).toNanos ();
85
- long intervalRemaining = interval ;
86
- final long interrupt = threadPoolConfig .shutdownInterrupt ().toNanos ();
87
- long interruptRemaining = interrupt ;
79
+ final long configShutdownTimeout = threadPoolConfig .shutdownTimeout ().toNanos ();
80
+ final long configShutdownInterrupt = threadPoolConfig .shutdownInterrupt ().toNanos ();
81
+ final long configShutdownCheckInterval = threadPoolConfig .shutdownCheckInterval ()
82
+ .orElse (Duration .ofNanos (Long .MAX_VALUE )).toNanos ();
83
+ long shutdownTimeout = configShutdownTimeout ;
84
+ long shutdownInterrupt = configShutdownInterrupt ;
85
+ long shutdownCheckInterval = configShutdownCheckInterval ;
88
86
89
87
long start = System .nanoTime ();
90
88
int loop = 1 ;
91
89
for (;;) {
92
90
// This log can be very useful when debugging problems
93
- log .debugf ("loop: %s, remaining : %s, intervalRemaining : %s, interruptRemaining : %s" , loop ++, remaining ,
94
- intervalRemaining , interruptRemaining );
91
+ log .debugf ("loop: %s, shutdownTimeout : %s, shutdownCheckInterval : %s, shutdownInterrupt : %s" , loop ++,
92
+ shutdownTimeout , shutdownCheckInterval , shutdownInterrupt );
95
93
try {
96
- if (!executor .awaitTermination (Math .min (remaining , intervalRemaining ), TimeUnit .NANOSECONDS )) {
94
+ if (!executor .awaitTermination (Math .min (shutdownTimeout , shutdownCheckInterval ), TimeUnit .NANOSECONDS )) {
97
95
long elapsed = System .nanoTime () - start ;
98
- intervalRemaining -= elapsed ;
99
- remaining = shutdownRemaining - elapsed ;
100
- interruptRemaining = interrupt - elapsed ;
101
- if (interruptRemaining <= 0 ) {
96
+ shutdownTimeout = configShutdownTimeout - elapsed ;
97
+ shutdownInterrupt = configShutdownInterrupt - elapsed ;
98
+ shutdownCheckInterval = configShutdownCheckInterval - elapsed ;
99
+ if (shutdownInterrupt <= 0 ) {
102
100
executor .shutdown (true );
103
101
}
104
- if (remaining <= 0 ) {
102
+ if (shutdownTimeout <= 0 ) {
105
103
// done waiting
106
104
final List <Runnable > runnables = executor .shutdownNow ();
107
105
if (!runnables .isEmpty ()) {
@@ -113,8 +111,8 @@ public void run() {
113
111
}
114
112
break ;
115
113
}
116
- if (intervalRemaining <= 0 ) {
117
- intervalRemaining = interval ;
114
+ if (shutdownCheckInterval <= 0 ) {
115
+ shutdownCheckInterval = configShutdownCheckInterval ;
118
116
// do some probing
119
117
final int queueSize = executor .getQueueSize ();
120
118
final Thread [] runningThreads = executor .getRunningThreads ();
0 commit comments