com.devexperts.util
Class LockFreePool<E>

java.lang.Object
  extended by com.devexperts.util.LockFreePool<E>

public class LockFreePool<E>
extends Object

A collection designed for pooling elements so that they can be reused later.

The LockFreePool is a fixed-capacity (or "bounded") stack as it orders elements in a LIFO (last-in-first-out) manner. The LockFreePool is a thread-safe and provides lock-free access.


Field Summary
static int MAX_CAPACITY
          Maximal supported capacity.
 
Constructor Summary
LockFreePool(int capacity)
          Creates a LockFreePool with the specified (fixed) capacity.
 
Method Summary
 boolean offer(E o)
          Inserts the specified element into this pool, if possible.
 E poll()
          Retrieves and removes the element from this pool, or null if this pool is empty.
 int size()
          Returns the number of elements in this pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_CAPACITY

public static final int MAX_CAPACITY
Maximal supported capacity.

See Also:
Constant Field Values
Constructor Detail

LockFreePool

public LockFreePool(int capacity)
Creates a LockFreePool with the specified (fixed) capacity.

Parameters:
capacity - the capacity of this pool
Throws:
IllegalArgumentException - if capacity is less than 1 or greater than MAX_CAPACITY
Method Detail

size

public int size()
Returns the number of elements in this pool.

Returns:
the number of elements in this pool

poll

public E poll()
Retrieves and removes the element from this pool, or null if this pool is empty.

Returns:
the element from this pool, or null if this pool is empty

offer

public boolean offer(E o)
Inserts the specified element into this pool, if possible.

Parameters:
o - the element to insert
Returns:
true if element was inserted into this pool, else false
Throws:
NullPointerException - if element is null
IllegalStateException - if element is already in the pool (best effort check only)


Copyright © 2013 Devexperts. All Rights Reserved.