Show / Hide Table of Contents

Class ConcurrentObjectPool<T>

Provides container for the thread-unsafe objects that can be shared between threads concurrently.

Inheritance
Object
ConcurrentObjectPool<T>
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
[Obsolete("Use Microsoft.Extensions.ObjectPool library instead")]
public class ConcurrentObjectPool<T> : Disposable where T : class
Type Parameters
Name Description
T

Type of objects in the pool.

Remarks

The object pool implements two scheduling strategies:

  1. Round-robin. This strategy requires that all objects should be created and initialized before instantiation of object pool. All objects are placed into pool and shared between concurrent threads. Workload is distributed across all objects in the pool in circular order. This strategy is recommended for situations when workload is constant or unpredictable, cost of the object in the pool is relatively low.
  2. Shortest Job First. This strategy instantiates objects in the pool on-demand depends on workload. The first released object will be passed to the one of waiting threads. Fairness policy is not supported so the longest waiting thread may not obtain the object first. Moreover, if the created object is not used for a long period of time then object pool will dispose it and remove the object from the pool. This strategy is recommended for situations when workload is variable and predictable (for instance, Poisson distribution of requests), cost of the object in the pool is high.
Object pool is compatible with async methods so it is possible to rent the object is one thread and return it back to the pool in another thread.

Constructors

| Improve this Doc View Source

ConcurrentObjectPool(IEnumerable<T>)

Initializes object pool that will apply Round-robing scheduling strategy.

Declaration
public ConcurrentObjectPool(IEnumerable<T> objects)
Parameters
Type Name Description
IEnumerable<T> objects

The objects to be placed into the pool.

Exceptions
Type Condition
ArgumentException

objects is empty.

See Also
Round-robin
| Improve this Doc View Source

ConcurrentObjectPool(Int32, Func<T>)

Initializes object pool that will apply Shortest Job First scheduling strategy.

Declaration
public ConcurrentObjectPool(int capacity, Func<T> factory)
Parameters
Type Name Description
Int32 capacity

The maximum objects in the pool.

Func<T> factory

The delegate instance that is used for lazy instantiation of objects in the pool.

Exceptions
Type Condition
ArgumentOutOfRangeException

capacity is less than zero.

ArgumentNullException

factory is null.

See Also
Shortest Job First
| Improve this Doc View Source

ConcurrentObjectPool(Int32, ValueFunc<T>)

Initializes object pool that will apply Shortest Job First scheduling strategy.

Declaration
public ConcurrentObjectPool(int capacity, ValueFunc<T> factory)
Parameters
Type Name Description
Int32 capacity

The maximum objects in the pool.

ValueFunc<T> factory

The delegate instance that is used for lazy instantiation of objects in the pool.

Exceptions
Type Condition
ArgumentOutOfRangeException

capacity is less than zero.

ArgumentException

factory is empty.

See Also
Shortest Job First

Properties

| Improve this Doc View Source

Capacity

Gets total count of objects in this pool.

Declaration
public int Capacity { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

WaitCount

Gets number of threads waiting for the first available object.

Declaration
public int WaitCount { get; }
Property Value
Type Description
Int32
Remarks

This property is for diagnostics purposes. Ideally, it should be always 0. But in reality, some threads may wait for the first released object a very small amount of time. Therefore, the expected value should not be greater than Capacity divided by 2, and do not grow over time. Otherwise, you should increase the capacity.

Methods

| Improve this Doc View Source

Dispose(Boolean)

Releases all resources associated with this object pool.

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
Boolean disposing

true if called from ; false if called from finalizer .

Remarks

This method is not thread-safe and may not be used concurrently with other members of this instance. Additionally, this method disposes all objects stored in the pool if it was created with ConcurrentObjectPool(Int32, Func<T>) constructor.

| Improve this Doc View Source

Rent()

Rents the object from this pool.

Declaration
public ConcurrentObjectPool<T>.IRental Rent()
Returns
Type Description
ConcurrentObjectPool.IRental<>

The object allows to control lifetime of the rent.

| Improve this Doc View Source

Rent(CancellationToken)

Rents the object from this pool.

Declaration
public ConcurrentObjectPool<T>.IRental Rent(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ConcurrentObjectPool.IRental<>

The object allows to control lifetime of the rent.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

Extension Methods

AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireUpgradeableReadLockAsync<T>(T, TimeSpan)
AsyncLockAcquisition.AcquireUpgradeableReadLockAsync<T>(T, CancellationToken)
ObjectExtensions.GetUserData<T>(T)
ObjectExtensions.IsOneOf<T>(T, IEnumerable<T>)
ObjectExtensions.IsOneOf<T>(T, T[])
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, Func<T, TResult1>, Func<T, TResult2>, out TResult1, out TResult2)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, ValueFunc<T, TResult1>, ValueFunc<T, TResult2>, out TResult1, out TResult2)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, Func<T, TResult1>, Func<T, TResult2>)
ObjectExtensions.Decompose<T, TResult1, TResult2>(T, ValueFunc<T, TResult1>, ValueFunc<T, TResult2>)
ObjectExtensions.As<T>(T)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX