Code example for AtomicInteger
Methods: compareAndSet, get
* the thread that constructed the region. It is thread-safe against other
* threads calling alloc(), who will block until the allocation is complete.
public void init()
assert nextFreeOffset.get() == UNINITIALIZED;
data = ByteBuffer.allocate(size);
assert data.remaining() == data.capacity();
// Mark that it's ready for use
boolean initted = nextFreeOffset.compareAndSet(UNINITIALIZED, 0);
// We should always succeed the above CAS since only one thread calls init()!
Preconditions.checkState(initted, "Multiple threads tried to init same region");
* Try to allocate <code>size</code> bytes from the region.
* @return the successful allocation, or null to indicate not-enough-space
public ByteBuffer allocate(int size)