improvement: Optimized NioReactor stop() (Reactor Pattern) (#1930)

* Optimized NioReactor stop()

* Optimized ThreadPoolDispatcher stop()
This commit is contained in:
CharlieYu 2021-12-24 00:10:17 +08:00 committed by GitHub
parent 4dcc20b733
commit 69883196d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -96,9 +96,11 @@ public class NioReactor {
* @throws IOException if any I/O error occurs. * @throws IOException if any I/O error occurs.
*/ */
public void stop() throws InterruptedException, IOException { public void stop() throws InterruptedException, IOException {
reactorMain.shutdownNow(); reactorMain.shutdown();
selector.wakeup(); selector.wakeup();
reactorMain.awaitTermination(4, TimeUnit.SECONDS); if (!reactorMain.awaitTermination(4, TimeUnit.SECONDS)) {
reactorMain.shutdownNow();
}
selector.close(); selector.close();
LOGGER.info("Reactor stopped"); LOGGER.info("Reactor stopped");
} }

View File

@ -64,6 +64,8 @@ public class ThreadPoolDispatcher implements Dispatcher {
@Override @Override
public void stop() throws InterruptedException { public void stop() throws InterruptedException {
executorService.shutdown(); executorService.shutdown();
executorService.awaitTermination(4, TimeUnit.SECONDS); if (executorService.awaitTermination(4, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} }
} }