diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioBossPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioBossPool.java index 90bdc2c591..08179061be 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioBossPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioBossPool.java @@ -22,6 +22,7 @@ import org.jboss.netty.util.internal.ExecutorUtil; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; public abstract class AbstractNioBossPool @@ -37,7 +38,7 @@ public abstract class AbstractNioBossPool private final Boss[] bosses; private final AtomicInteger bossIndex = new AtomicInteger(); private final Executor bossExecutor; - private volatile boolean initialized; + private final AtomicBoolean initialized = new AtomicBoolean(false); /** * Create a new instance @@ -66,10 +67,9 @@ public abstract class AbstractNioBossPool } protected void init() { - if (initialized) { + if (!initialized.compareAndSet(false, true)) { throw new IllegalStateException("initialized already"); } - initialized = true; for (int i = 0; i < bosses.length; i++) { bosses[i] = newBoss(bossExecutor); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioWorkerPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioWorkerPool.java index 6b6f0e1fa5..ad8787c95c 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioWorkerPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioWorkerPool.java @@ -24,6 +24,7 @@ import org.jboss.netty.util.internal.ExecutorUtil; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; /** @@ -43,7 +44,7 @@ public abstract class AbstractNioWorkerPool private final AbstractNioWorker[] workers; private final AtomicInteger workerIndex = new AtomicInteger(); private final Executor workerExecutor; - private volatile boolean initialized; + private final AtomicBoolean initialized = new AtomicBoolean(false); /** * Create a new instance @@ -71,12 +72,10 @@ public abstract class AbstractNioWorkerPool } protected void init() { - if (initialized) { + if (!initialized.compareAndSet(false, true)) { throw new IllegalStateException("initialized already"); } - initialized = true; - for (int i = 0; i < workers.length; i++) { workers[i] = newWorker(workerExecutor); }