diff --git a/client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java b/client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java index 522fc74620..771f7e55e1 100755 --- a/client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java +++ b/client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java @@ -15,11 +15,6 @@ import static org.asynchttpclient.util.Assertions.assertNotNull; import static org.asynchttpclient.util.DateUtils.unpreciseMillisTime; -import io.netty.channel.Channel; -import io.netty.channel.ChannelId; -import io.netty.util.Timeout; -import io.netty.util.Timer; -import io.netty.util.TimerTask; import java.net.InetSocketAddress; import java.util.*; @@ -27,6 +22,7 @@ import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -36,6 +32,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.netty.channel.Channel; +import io.netty.channel.ChannelId; +import io.netty.util.Timeout; +import io.netty.util.Timer; +import io.netty.util.TimerTask; + /** * A simple implementation of {@link ChannelPool} based on a {@link java.util.concurrent.ConcurrentHashMap} */ @@ -106,9 +108,13 @@ private static final class ChannelCreation { } private static final class IdleChannel { + + private static final AtomicIntegerFieldUpdater ownedField = AtomicIntegerFieldUpdater.newUpdater(IdleChannel.class, "owned"); + final Channel channel; final long start; - final AtomicBoolean owned = new AtomicBoolean(false); + @SuppressWarnings("unused") + private volatile int owned = 0; IdleChannel(Channel channel, long start) { this.channel = assertNotNull(channel, "channel"); @@ -116,7 +122,7 @@ private static final class IdleChannel { } public boolean takeOwnership() { - return owned.compareAndSet(false, true); + return ownedField.getAndSet(this, 1) == 0; } public Channel getChannel() {