29
29
import java .io .IOException ;
30
30
import java .net .InetAddress ;
31
31
import java .net .ServerSocket ;
32
- import java .util .List ;
33
- import java .util .concurrent .ExecutorService ;
34
- import java .util .concurrent .Executors ;
32
+ import java .util .Set ;
33
+ import java .util .concurrent .SynchronousQueue ;
34
+ import java .util .concurrent .ThreadPoolExecutor ;
35
35
import java .util .concurrent .TimeUnit ;
36
36
import java .util .concurrent .atomic .AtomicReference ;
37
37
@@ -60,9 +60,9 @@ enum Status { READY, ACTIVE, STOPPING }
60
60
private final HttpConnectionFactory <? extends DefaultBHttpServerConnection > connectionFactory ;
61
61
private final SSLServerSetupHandler sslSetupHandler ;
62
62
private final ExceptionLogger exceptionLogger ;
63
- private final ExecutorService listenerExecutorService ;
63
+ private final ThreadPoolExecutor listenerExecutorService ;
64
64
private final ThreadGroup workerThreads ;
65
- private final ExecutorService workerExecutorService ;
65
+ private final WorkerPoolExecutor workerExecutorService ;
66
66
private final AtomicReference <Status > status ;
67
67
68
68
private volatile ServerSocket serverSocket ;
@@ -85,10 +85,14 @@ enum Status { READY, ACTIVE, STOPPING }
85
85
this .connectionFactory = connectionFactory ;
86
86
this .sslSetupHandler = sslSetupHandler ;
87
87
this .exceptionLogger = exceptionLogger ;
88
- this .listenerExecutorService = Executors .newSingleThreadExecutor (
88
+ this .listenerExecutorService = new ThreadPoolExecutor (
89
+ 1 , 1 , 0L , TimeUnit .MILLISECONDS ,
90
+ new SynchronousQueue <Runnable >(),
89
91
new ThreadFactoryImpl ("HTTP-listener-" + this .port ));
90
92
this .workerThreads = new ThreadGroup ("HTTP-workers" );
91
- this .workerExecutorService = Executors .newCachedThreadPool (
93
+ this .workerExecutorService = new WorkerPoolExecutor (
94
+ 0 , Integer .MAX_VALUE , 1L , TimeUnit .SECONDS ,
95
+ new SynchronousQueue <Runnable >(),
92
96
new ThreadFactoryImpl ("HTTP-worker" , this .workerThreads ));
93
97
this .status = new AtomicReference <Status >(Status .READY );
94
98
}
@@ -135,6 +139,8 @@ public void start() throws IOException {
135
139
136
140
public void stop () {
137
141
if (this .status .compareAndSet (Status .ACTIVE , Status .STOPPING )) {
142
+ this .listenerExecutorService .shutdown ();
143
+ this .workerExecutorService .shutdown ();
138
144
final RequestListener local = this .requestListener ;
139
145
if (local != null ) {
140
146
try {
@@ -144,8 +150,6 @@ public void stop() {
144
150
}
145
151
}
146
152
this .workerThreads .interrupt ();
147
- this .listenerExecutorService .shutdown ();
148
- this .workerExecutorService .shutdown ();
149
153
}
150
154
}
151
155
@@ -162,16 +166,13 @@ public void shutdown(final long gracePeriod, final TimeUnit timeUnit) {
162
166
Thread .currentThread ().interrupt ();
163
167
}
164
168
}
165
- final List <Runnable > runnables = this .workerExecutorService .shutdownNow ();
166
- for (Runnable runnable : runnables ) {
167
- if (runnable instanceof Worker ) {
168
- final Worker worker = (Worker ) runnable ;
169
- final HttpServerConnection conn = worker .getConnection ();
170
- try {
171
- conn .shutdown ();
172
- } catch (IOException ex ) {
173
- this .exceptionLogger .log (ex );
174
- }
169
+ final Set <Worker > workers = this .workerExecutorService .getWorkers ();
170
+ for (Worker worker : workers ) {
171
+ final HttpServerConnection conn = worker .getConnection ();
172
+ try {
173
+ conn .shutdown ();
174
+ } catch (IOException ex ) {
175
+ this .exceptionLogger .log (ex );
175
176
}
176
177
}
177
178
}
0 commit comments