Top Description Inners Fields Constructors Methods
io.netty.util

public abstract Class Recycler<T>

extends Object
Class Inheritance
Type Parameters
<T>
the type of the pooled object
Imports
io.netty.util.concurrent.FastThreadLocal, .FastThreadLocalThread, io.netty.util.internal.ObjectPool, .PlatformDependent, .SystemPropertyUtil, .UnstableApi, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue, org.jetbrains.annotations.VisibleForTesting, java.util.ArrayDeque, .Queue, java.util.concurrent.atomic.AtomicIntegerFieldUpdater

Light-weight object pool based on a thread-local stack.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Recycler.BlockingMessageQueue<T>

This is an implementation of MessagePassingQueue, similar to what might be returned from PlatformDependent#newMpscQueue(int), but intended to be used for debugging purpose.

private static class
public abstract static class
public static interface
private static class

Field Summary

Modifier and TypeField and Description
private static final boolean
private static final boolean
private final int
private static final int
private static final int
private static final int
private final int
private static final InternalLogger
private final int
private static final Recycler.EnhancedHandle<?>
private static final int
private final FastThreadLocal<Recycler.LocalPool<T>>

Constructor Summary

AccessConstructor and Description
protected
protected
Recycler(int maxCapacityPerThread)

protected
Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor)

Deprecated Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).
protected
Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread)

Deprecated Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).
protected
Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread, int delayedQueueRatio)

Deprecated Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).
protected
Recycler(int maxCapacityPerThread, int ratio, int chunkSize)

Method Summary

Modifier and TypeMethod and Description
public final T
get()

protected abstract T
newObject(Recycler.Handle<T>
can NOT be null.
handle
)

public final boolean
recycle(T o, Recycler.Handle<T> handle)

Deprecated use Handle#recycle(Object).
pack-priv final int
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BATCH_FAST_TL_ONLYback to summary
private static final boolean BATCH_FAST_TL_ONLY
BLOCKING_POOLback to summary
private static final boolean BLOCKING_POOL
chunkSizeback to summary
private final int chunkSize
DEFAULT_INITIAL_MAX_CAPACITY_PER_THREADback to summary
private static final int DEFAULT_INITIAL_MAX_CAPACITY_PER_THREAD
DEFAULT_MAX_CAPACITY_PER_THREADback to summary
private static final int DEFAULT_MAX_CAPACITY_PER_THREAD
DEFAULT_QUEUE_CHUNK_SIZE_PER_THREADback to summary
private static final int DEFAULT_QUEUE_CHUNK_SIZE_PER_THREAD
intervalback to summary
private final int interval
loggerback to summary
private static final InternalLogger logger
maxCapacityPerThreadback to summary
private final int maxCapacityPerThread
NOOP_HANDLEback to summary
private static final Recycler.EnhancedHandle<?> NOOP_HANDLE
RATIOback to summary
private static final int RATIO
threadLocalback to summary
private final FastThreadLocal<Recycler.LocalPool<T>> threadLocal

Constructor Detail

Recyclerback to summary
protected Recycler()
Recyclerback to summary
protected Recycler(int maxCapacityPerThread)
Recyclerback to summary
protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor)

Deprecated

Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).

Annotations
@Deprecated
@SuppressWarnings:unused
Recyclerback to summary
protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread)

Deprecated

Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).

Annotations
@Deprecated
@SuppressWarnings:unused
Recyclerback to summary
protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor, int ratio, int maxDelayedQueuesPerThread, int delayedQueueRatio)

Deprecated

Use one of the following instead: Recycler(), Recycler(int), Recycler(int, int, int).

Annotations
@Deprecated
@SuppressWarnings:unused
Recyclerback to summary
protected Recycler(int maxCapacityPerThread, int ratio, int chunkSize)

Method Detail

getback to summary
public final T get()
Annotations
@SuppressWarnings:unchecked
newObjectback to summary
protected abstract T newObject(Recycler.Handle<T> handle)
Parameters
handle:Recycler.Handle<T>

can NOT be null.

recycleback to summary
public final boolean recycle(T o, Recycler.Handle<T> handle)

Deprecated

use Handle#recycle(Object).

Annotations
@Deprecated
threadLocalSizeback to summary
pack-priv final int threadLocalSize()
Annotations
@VisibleForTesting
io.netty.util back to summary

private final Class Recycler.BlockingMessageQueue<T>

extends Object
implements MessagePassingQueue<T>
Class Inheritance
All Implemented Interfaces
io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue

This is an implementation of MessagePassingQueue, similar to what might be returned from PlatformDependent#newMpscQueue(int), but intended to be used for debugging purpose. The implementation relies on synchronised monitor locks for thread-safety. The fill bulk operation is not supported by this implementation.

Field Summary

Modifier and TypeField and Description
private final Queue<T>
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
BlockingMessageQueue(int maxCapacity)

Method Summary

Modifier and TypeMethod and Description
public int
public synchronized void
public int
drain(MessagePassingQueue.Consumer<T> c, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove up to limit elements from the queue and hand to consume.
public int
drain(MessagePassingQueue.Consumer<T> c)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove all available item from the queue and hand to consume.
public void
public int
fill(MessagePassingQueue.Supplier<T> s, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with up to limit elements from the supplier.
public int
fill(MessagePassingQueue.Supplier<T> s)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier.
public void
public synchronized boolean
isEmpty()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.isEmpty.

This method's accuracy is subject to concurrent modifications happening as the observation is carried out.
public synchronized boolean
offer(T
not null, will throw NPE if it is
e
)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer.

Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue#offer(Object) interface.
public synchronized T
peek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#peek() interface.
public synchronized T
poll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#poll() interface.
public boolean
relaxedOffer(T
not null, will throw NPE if it is
e
)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Called from a producer thread subject to the restrictions appropriate to the implementation.
public T
relaxedPeek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Called from the consumer thread subject to the restrictions appropriate to the implementation.
public T
relaxedPoll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Called from the consumer thread subject to the restrictions appropriate to the implementation.
public synchronized int
size()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.size.

This method's accuracy is subject to concurrent modifications happening as the size is estimated and as such is a best effort rather than absolute value.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dequeback to summary
private final Queue<T> deque
maxCapacityback to summary
private final int maxCapacity

Constructor Detail

BlockingMessageQueueback to summary
pack-priv BlockingMessageQueue(int maxCapacity)

Method Detail

capacityback to summary
public int capacity()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.capacity.

Returns:int

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.capacity.

the capacity of this queue or MessagePassingQueue#UNBOUNDED_CAPACITY if not bounded

Annotations
@Override
clearback to summary
public synchronized void clear()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.clear.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.clear.

Removes all items from the queue. Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#clear() interface.

Annotations
@Override
drainback to summary
public int drain(MessagePassingQueue.Consumer<T> c, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove up to limit elements from the queue and hand to consume. This should be semantically similar to:

M m;
  int i = 0;
  for(;i < limit && (m = relaxedPoll()) != null; i++){
    c.accept(m);
  }
  return i;

There's no strong commitment to the queue being empty at the end of a drain. Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Returns:int

the number of polled elements

Annotations
@Override
drainback to summary
public int drain(MessagePassingQueue.Consumer<T> c)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove all available item from the queue and hand to consume. This should be semantically similar to:

M m;
while((m = relaxedPoll()) != null){
c.accept(m);
}
There's no strong commitment to the queue being empty at the end of a drain. Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Returns:int

the number of polled elements

Annotations
@Override
drainback to summary
public void drain(MessagePassingQueue.Consumer<T> c, MessagePassingQueue.WaitStrategy wait, MessagePassingQueue.ExitCondition exit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.drain.

Remove elements from the queue and hand to consume forever. Semantically similar to:

 int idleCounter = 0;
 while (exit.keepRunning()) {
     E e = relaxedPoll();
     if(e==null){
         idleCounter = wait.idle(idleCounter);
         continue;
     }
     idleCounter = 0;
     c.accept(e);
 }

Called from a consumer thread subject to the restrictions appropriate to the implementation.

Warning

Explicit assumptions are made with regards to Consumer#accept make sure you have read and understood these before using this method.

Annotations
@Override
fillback to summary
public int fill(MessagePassingQueue.Supplier<T> s, int limit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with up to limit elements from the supplier. Semantically similar to:

for(int i=0; i < limit && relaxedOffer(s.get()); i++);

There's no strong commitment to the queue being full at the end of a fill. Called from a producer thread subject to the restrictions appropriate to the implementation

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Returns:int

the number of offered elements

Annotations
@Override
fillback to summary
public int fill(MessagePassingQueue.Supplier<T> s)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier. Semantically similar to:

while(relaxedOffer(s.get());
There's no strong commitment to the queue being full at the end of a fill. Called from a producer thread subject to the restrictions appropriate to the implementation.

Unbounded queues will fill up the queue with a fixed amount rather than fill up to oblivion

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Returns:int

the number of offered elements

Annotations
@Override
fillback to summary
public void fill(MessagePassingQueue.Supplier<T> s, MessagePassingQueue.WaitStrategy wait, MessagePassingQueue.ExitCondition exit)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.fill.

Stuff the queue with elements from the supplier forever. Semantically similar to:


 int idleCounter = 0;
 while (exit.keepRunning()) {
     E e = s.get();
     while (!relaxedOffer(e)) {
         idleCounter = wait.idle(idleCounter);
         continue;
     }
     idleCounter = 0;
 }

Called from a producer thread subject to the restrictions appropriate to the implementation. The main difference being that implementors MUST assure room in the queue is available BEFORE calling Supplier#get

Warning

Explicit assumptions are made with regards to Supplier#get make sure you have read and understood these before using this method.

Annotations
@Override
isEmptyback to summary
public synchronized boolean isEmpty()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.isEmpty.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.isEmpty.

This method's accuracy is subject to concurrent modifications happening as the observation is carried out.

Returns:boolean

true if empty, false otherwise

Annotations
@Override
offerback to summary
public synchronized boolean offer(T e)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.offer.

Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue#offer(Object) interface.

Parameters
e:T

not null, will throw NPE if it is

Returns:boolean

true if element was inserted into the queue, false iff full

Annotations
@Override
peekback to summary
public synchronized T peek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.peek.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#peek() interface.

Returns:T

a message from the queue if one is available, null iff empty

Annotations
@Override
pollback to summary
public synchronized T poll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.poll.

Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue#poll() interface.

Returns:T

a message from the queue if one is available, null iff empty

Annotations
@Override
relaxedOfferback to summary
public boolean relaxedOffer(T e)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedOffer.

Called from a producer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#offer(Object) this method may return false without the queue being full.

Parameters
e:T

not null, will throw NPE if it is

Returns:boolean

true if element was inserted into the queue, false if unable to offer

Annotations
@Override
relaxedPeekback to summary
public T relaxedPeek()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPeek.

Called from the consumer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#peek() this method may return null without the queue being empty.

Returns:T

a message from the queue if one is available, null if unable to peek

Annotations
@Override
relaxedPollback to summary
public T relaxedPoll()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.relaxedPoll.

Called from the consumer thread subject to the restrictions appropriate to the implementation. As opposed to Queue#poll() this method may return null without the queue being empty.

Returns:T

a message from the queue if one is available, null if unable to poll

Annotations
@Override
sizeback to summary
public synchronized int size()

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.size.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.size.

This method's accuracy is subject to concurrent modifications happening as the size is estimated and as such is a best effort rather than absolute value. For some implementations this method may be O(n) rather than O(1).

Returns:int

number of messages in the queue, between 0 and Integer#MAX_VALUE but less or equals to capacity (if bounded).

Annotations
@Override
io.netty.util back to summary

private final Class Recycler.DefaultHandle<T>

extends Recycler.EnhancedHandle<T>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Recycler.LocalPool<T>
private volatile int
private static final int
private static final int
private static final AtomicIntegerFieldUpdater<Recycler.DefaultHandle<?>>
private T

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv T
get()

public void
recycle(Object object)

Implements io.netty.util.internal.ObjectPool.Handle.recycle.

Recycle the Object if possible and so make it ready to be reused.
pack-priv void
set(T value)

pack-priv void
pack-priv void
public void
pack-priv void

Field Detail

localPoolback to summary
private final Recycler.LocalPool<T> localPool
stateback to summary
private volatile int state
STATE_AVAILABLEback to summary
private static final int STATE_AVAILABLE
STATE_CLAIMEDback to summary
private static final int STATE_CLAIMED
STATE_UPDATERback to summary
private static final AtomicIntegerFieldUpdater<Recycler.DefaultHandle<?>> STATE_UPDATER
valueback to summary
private T value

Constructor Detail

DefaultHandleback to summary
pack-priv DefaultHandle(Recycler.LocalPool<T> localPool)

Method Detail

getback to summary
pack-priv T get()
recycleback to summary
public void recycle(Object object)

Implements io.netty.util.internal.ObjectPool.Handle.recycle.

Doc from io.netty.util.internal.ObjectPool.Handle.recycle.

Recycle the Object if possible and so make it ready to be reused.

Annotations
@Override
setback to summary
pack-priv void set(T value)
toAvailableback to summary
pack-priv void toAvailable()
toClaimedback to summary
pack-priv void toClaimed()
unguardedRecycleback to summary
public void unguardedRecycle(Object object)

Implements abstract io.netty.util.Recycler.EnhancedHandle.unguardedRecycle.

Annotations
@Override
unguardedToAvailableback to summary
pack-priv void unguardedToAvailable()
io.netty.util back to summary

public abstract Class Recycler.EnhancedHandle<T>

extends Object
implements Recycler.Handle<T>
Class Inheritance
All Implemented Interfaces
io.netty.util.Recycler.Handle, io.netty.util.internal.ObjectPool.Handle
Known Direct Subclasses
io.netty.util.Recycler.DefaultHandle
Annotations
@UnstableApi

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public abstract void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

EnhancedHandleback to summary
private EnhancedHandle()

Method Detail

unguardedRecycleback to summary
public abstract void unguardedRecycle(Object object)
io.netty.util back to summary

public Interface Recycler.Handle<T>

extends ObjectPool.Handle<T>
Known Direct Implementers
io.netty.util.Recycler.EnhancedHandle
Annotations
@SuppressWarnings:ClassNameSameAsAncestorName

Method Summary

Inherited from io.netty.util.internal.ObjectPool.Handle:
recycle
io.netty.util back to summary

private final Class Recycler.LocalPool<T>

extends Object
implements MessagePassingQueue.Consumer<Recycler.DefaultHandle<T>>
Class Inheritance
All Implemented Interfaces
io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer

Field Summary

Modifier and TypeField and Description
private final ArrayDeque<Recycler.DefaultHandle<T>>
private final int
private volatile Thread
private volatile MessagePassingQueue<Recycler.DefaultHandle<T>>
private int
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
LocalPool(int maxCapacity, int ratioInterval, int chunkSize)

Method Summary

Modifier and TypeMethod and Description
public void
accept(Recycler.DefaultHandle<T>
not null
e
)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer.accept.

This method will process an element already removed from the queue.
pack-priv Recycler.DefaultHandle<T>
private static boolean
pack-priv Recycler.DefaultHandle<T>
pack-priv void
release(Recycler.DefaultHandle<T> handle, boolean guarded)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

batchback to summary
private final ArrayDeque<Recycler.DefaultHandle<T>> batch
chunkSizeback to summary
private final int chunkSize
ownerback to summary
private volatile Thread owner
pooledHandlesback to summary
private volatile MessagePassingQueue<Recycler.DefaultHandle<T>> pooledHandles
ratioCounterback to summary
private int ratioCounter
ratioIntervalback to summary
private final int ratioInterval

Constructor Detail

LocalPoolback to summary
pack-priv LocalPool(int maxCapacity, int ratioInterval, int chunkSize)
Annotations
@SuppressWarnings:unchecked

Method Detail

acceptback to summary
public void accept(Recycler.DefaultHandle<T> e)

Implements io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer.accept.

Doc from io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer.accept.

This method will process an element already removed from the queue. This method is expected to never throw an exception.

Users should be aware that underlying queue implementations may upfront claim parts of the queue for batch operations and this will effect the view on the queue from the accept method. In particular size and any poll/peek methods may take the view that the full batch has already happened.

Warning

this method is assumed to never throw. Breaking this assumption can lead to a broken queue.

Parameters
e:Recycler.DefaultHandle<T>

not null

Annotations
@Override
claimback to summary
pack-priv Recycler.DefaultHandle<T> claim()
isTerminatedback to summary
private static boolean isTerminated(Thread owner)
newHandleback to summary
pack-priv Recycler.DefaultHandle<T> newHandle()
releaseback to summary
pack-priv void release(Recycler.DefaultHandle<T> handle, boolean guarded)