| 28 | import java.util.concurrent.atomic.AtomicInteger; |
| 29 | |
| 30 | public class ConcurrentPool<T> { |
| 31 | @SuppressWarnings("rawtypes") |
| 32 | public static final ConcurrentSegmentManipulator POOL_MANIPULATOR = new ConcurrentSegmentManipulator() { |
| 33 | |
| 34 | @Override |
| 35 | public Object dequeue(ConcurrentQueueSegment.Slot[] slots, int slotsIndex, Object unused) { |
| 36 | var val = slots[slotsIndex].item; |
| 37 | slots[slotsIndex].item = null; |
| 38 | return val; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void enqueue(Object item, ConcurrentQueueSegment.Slot[] slots, int slotsIndex) { |
| 43 | slots[slotsIndex].item = item; |
| 44 | } |
| 45 | }; |
| 46 | private final AtomicInteger count = new AtomicInteger(0); |
| 47 | private final ConcurrentQueue<T> queue; |
| 48 | |
| 49 | public ConcurrentPool() { |
| 50 | //noinspection unchecked |
| 51 | this.queue = new ConcurrentQueue<T>(() -> null, POOL_MANIPULATOR); |
| 52 | } |
| 53 | |
| 54 | public int capacity() { |
| 55 | return queue.capacity(); |
| 56 | } |
| 57 | |
| 58 | public int count() { |
| 59 | return count.get(); |
| 60 | } |
| 61 | |
| 62 | public T pop() { |
| 63 | T val = queue.tryDequeueValue(null); |
| 64 | if (val != null) { |
| 65 | count.decrementAndGet(); |
| 66 | } |
| 67 | return val; |
| 68 | } |
| 69 | |
| 70 | public void push(T item) { |
| 71 | queue.enqueue(item); |
| 72 | count.incrementAndGet(); |
| 73 | } |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…