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,31 @@ 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 ),
95
+ TimeUnit .NANOSECONDS )) {
97
96
long elapsed = System .nanoTime () - start ;
98
- intervalRemaining -= elapsed ;
99
- remaining = shutdownRemaining - elapsed ;
100
- interruptRemaining = interrupt - elapsed ;
101
- if (interruptRemaining <= 0 ) {
97
+ shutdownTimeout = configShutdownTimeout - elapsed ;
98
+ shutdownInterrupt = configShutdownInterrupt - elapsed ;
99
+ shutdownCheckInterval = configShutdownCheckInterval - elapsed ;
100
+ if (shutdownInterrupt <= 0 ) {
102
101
executor .shutdown (true );
103
102
}
104
- if (remaining <= 0 ) {
103
+ if (shutdownTimeout <= 0 ) {
105
104
// done waiting
106
105
final List <Runnable > runnables = executor .shutdownNow ();
107
106
if (!runnables .isEmpty ()) {
@@ -113,8 +112,8 @@ public void run() {
113
112
}
114
113
break ;
115
114
}
116
- if (intervalRemaining <= 0 ) {
117
- intervalRemaining = interval ;
115
+ if (shutdownCheckInterval <= 0 ) {
116
+ shutdownCheckInterval = configShutdownCheckInterval ;
118
117
// do some probing
119
118
final int queueSize = executor .getQueueSize ();
120
119
final Thread [] runningThreads = executor .getRunningThreads ();
0 commit comments