diff --git a/src/main/java/com/github/luben/zstd/RecyclingBufferPool.java b/src/main/java/com/github/luben/zstd/RecyclingBufferPool.java index c702bb0..907f9de 100644 --- a/src/main/java/com/github/luben/zstd/RecyclingBufferPool.java +++ b/src/main/java/com/github/luben/zstd/RecyclingBufferPool.java @@ -2,8 +2,7 @@ import java.lang.ref.SoftReference; import java.nio.ByteBuffer; -import java.util.ArrayDeque; -import java.util.Deque; +import java.util.concurrent.ConcurrentLinkedQueue; /** * A pool of buffers which uses a simple reference queue to recycle buffers. @@ -15,15 +14,14 @@ public class RecyclingBufferPool implements BufferPool { public static final BufferPool INSTANCE = new RecyclingBufferPool(); private static final int buffSize = Math.max(Math.max( - (int) ZstdOutputStreamNoFinalizer.recommendedCOutSize(), - (int) ZstdInputStreamNoFinalizer.recommendedDInSize()), + (int) ZstdOutputStreamNoFinalizer.recommendedCOutSize(), + (int) ZstdInputStreamNoFinalizer.recommendedDInSize()), (int) ZstdInputStreamNoFinalizer.recommendedDOutSize()); - private final Deque> pool; + private final ConcurrentLinkedQueue> pool; private RecyclingBufferPool() { - // TODO: With Java 7 support, migrate this to a ConcurrentLinkedQueue and remove the 'synchronization' of it. - this.pool = new ArrayDeque>(); + this.pool = new ConcurrentLinkedQueue<>(); } @Override @@ -31,20 +29,14 @@ public ByteBuffer get(int capacity) { if (capacity > buffSize) { throw new RuntimeException( "Unsupported buffer size: " + capacity + - ". Supported buffer sizes: " + buffSize + " or smaller." - ); + ". Supported buffer sizes: " + buffSize + " or smaller." + ); } while(true) { - SoftReference sbuf = null; - // This if statement introduces a possible race condition of allocating a buffer while we're trying to // release one. However, the extra allocation should be considered insignificant in terms of cost. // Particularly with respect to throughput. - if (!pool.isEmpty()) { - synchronized (pool) { - sbuf = pool.pollFirst(); - } - } + SoftReference sbuf = pool.poll(); if (sbuf == null) { return ByteBuffer.allocate(buffSize); @@ -60,9 +52,7 @@ public ByteBuffer get(int capacity) { public void release(ByteBuffer buffer) { if (buffer.capacity() >= buffSize) { buffer.clear(); - synchronized (pool) { - pool.addLast(new SoftReference(buffer)); - } + pool.add(new SoftReference<>(buffer)); } } }