Class ConcurrentObjectPool<T>
Provides container for the thread-unsafe objects that can be shared between threads concurrently.
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
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:
- 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.
- 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.
Constructors
| Improve this Doc View SourceConcurrentObjectPool(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. |
See Also
| Improve this Doc View SourceConcurrentObjectPool(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. |
See Also
| Improve this Doc View SourceConcurrentObjectPool(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. |
See Also
Properties
| Improve this Doc View SourceCapacity
Gets total count of objects in this pool.
Declaration
public int Capacity { get; }
Property Value
Type | Description |
---|---|
Int32 |
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 SourceDispose(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 |
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.
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. |
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. |