diff --git a/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java b/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java index 56fd442b66..ab2a914c69 100644 --- a/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java @@ -595,19 +595,8 @@ public abstract class SingleThreadEventExecutor extends AbstractScheduledEventEx gracefulShutdownQuietPeriod = unit.toNanos(quietPeriod); gracefulShutdownTimeout = unit.toNanos(timeout); - if (oldState == ST_NOT_STARTED) { - try { - doStartThread(); - } catch (Throwable cause) { - STATE_UPDATER.set(this, ST_TERMINATED); - terminationFuture.tryFailure(cause); - - if (!(cause instanceof Exception)) { - // Also rethrow as it may be an OOME for example - PlatformDependent.throwException(cause); - } - return terminationFuture; - } + if (ensureThreadStarted(oldState)) { + return terminationFuture; } if (wakeup) { @@ -658,19 +647,8 @@ public abstract class SingleThreadEventExecutor extends AbstractScheduledEventEx } } - if (oldState == ST_NOT_STARTED) { - try { - doStartThread(); - } catch (Throwable cause) { - STATE_UPDATER.set(this, ST_TERMINATED); - terminationFuture.tryFailure(cause); - - if (!(cause instanceof Exception)) { - // Also rethrow as it may be an OOME for example - PlatformDependent.throwException(cause); - } - return; - } + if (ensureThreadStarted(oldState)) { + return; } if (wakeup) { @@ -893,6 +871,24 @@ public abstract class SingleThreadEventExecutor extends AbstractScheduledEventEx } } + private boolean ensureThreadStarted(int oldState) { + if (oldState == ST_NOT_STARTED) { + try { + doStartThread(); + } catch (Throwable cause) { + STATE_UPDATER.set(this, ST_TERMINATED); + terminationFuture.tryFailure(cause); + + if (!(cause instanceof Exception)) { + // Also rethrow as it may be an OOME for example + PlatformDependent.throwException(cause); + } + return true; + } + } + return false; + } + private void doStartThread() { assert thread == null; executor.execute(new Runnable() {