BaseObjectPool<TItem>

Overview

The BaseObjectPool class is a base class for all object pool classes. It implements the IObjectPool interface and provides methods to acquire and release instances and operate elements of the object pool.

The BaseObjectPool is a generic class with one param TItem. This parameter specifies the type of pool's objects and should be a class.

This is an abstract class that cannot be instantiated directly. Instead, descendants of this class should be used where the OnCreateInstance method is implemented. For example: the ObjectPool generic class should be used for types that provide parameterless constructors. At the same time, the CustomObjectPool generic class provides a constructor that accepts a delegate for instantiating object pool elements, so more advanced instantiating logic can be used.

The BaseObjectPool class implements the IObjectPool, IDisposable, IEnumerable and IEnumerator interfaces.

Location


 

constructor  protected

Creates a new instance of the BaseObjectPool class.

The created pool will have the size -1 (i.e. infinite).

Cannot be called directly.

 

constructor

 

BaseObjectPool<TItem>()

 

Sub New()

constructor (PoolBehavior, Int32, Int32)  protected

 

constructor(behavior: PoolBehavior; maxSize: Int32; timeout: Int32)

 

BaseObjectPool<TItem>(PoolBehavior behavior, Int32 maxSize, Int32 timeout)

 

Sub New(behavior As PoolBehavior, maxSize As Int32, timeout As Int32)

Parameters:

  • behavior:
  • maxSize:
  • timeout:

AcquiredInstancesCount

Gets the count of the currently acquired object pool elements.

 

property AcquiredInstancesCount: Int32 read;

 

Int32 AcquiredInstancesCount { get; }

 

ReadOnly Property AcquiredInstancesCount() As Int32

AcquireInstance

Acquires an item from the pool.

If there are non-acquired pool items or pool has an infinite size, the pool item is acquired and returned to the caller. Otherwise, the Behavior property specifies the result of the method call:

  • PoolBehavior.Wait - wait until any pool item becomes available.
  • PoolBehavior.IgnoreAndReturn - wait for no more than WaitTimeOut milliseconds for any pool items. If there still are no pool items available after timeout, null will be returned.
  • PoolBehavior.Raise - wait for no more than WaitTimeOut milliseconds for any pool items. If there still are no pool items available after timeout, an NoFreeObjectsInPoolException is raised.

 

method AcquireInstance: TItem

 

TItem AcquireInstance()

 

Function AcquireInstance() As TItem

Behavior

Specifies the behavior of the object pool when an item is requested but the pool is empty (i.e. all available elements are acquired).

See the description of the AcquireInstance method for more details about possible Behavior settings.

 

property Behavior: PoolBehavior read write;

 

PoolBehavior Behavior { get; set; }

 

Property Behavior() As PoolBehavior

Clear

Clears all items from the pool.

 

method Clear

 

void Clear()

 

Sub Clear()

Current

Gets the current element in the collection.

This property is a part of the IEnumerator interface implementation.

 

property Current: Object read;

 

Object Current { get; }

 

ReadOnly Property Current() As Object

Dispose

Releases all pool resources.

The FinalizePool method can be overridden in case some specific cleanup actions are needed.

 

method Dispose

 

void Dispose()

 

Sub Dispose()

DropInstance

Removes the anItem object from the pool.

 

method DropInstance(item: TItem)

 

void DropInstance(TItem item)

 

Sub DropInstance(item As TItem)

Parameters:

  • item:

ExposeNonAcquiredInstances

Exposes all non-acquired pool objects using the specified aDelegate.

 

method ExposeNonAcquiredInstances(action: ExposeNonAcquiredInstancesDelegate<TItem>)

 

void ExposeNonAcquiredInstances(ExposeNonAcquiredInstancesDelegate<TItem> action)

 

Sub ExposeNonAcquiredInstances(action As ExposeNonAcquiredInstancesDelegate<TItem>)

Parameters:

  • action:

FinalizePool  protected

Finalizes the object pool.

This method's implementation is empty in the BaseObjectPool class but can be overriden in derived classes in case some specific cleanup actions are needed.

 

method FinalizePool

 

void FinalizePool()

 

Sub FinalizePool()

GetEnumerator

Returns an enumerator that iterates through a collection of non-acquired pool objects.

This method is part of the IEnumerable interface implementation.

 

method GetEnumerator: IEnumerator

 

IEnumerator GetEnumerator()

 

Function GetEnumerator() As IEnumerator

Instances  protected

Gets all pooled instances as a linked list.

This property can be used in the BaseObjectPool descendants to fine-tune pooled instances etc.

 

property Instances: LinkedList<TItem> read;

 

LinkedList<TItem> Instances { get; }

 

ReadOnly Property Instances() As LinkedList<TItem>

MaxPoolSize

Gets the maximum number of items that can be contained in the pool.

-1 means the pool is of unlimited size.

It is not possible to set this property to a positive value less than the AcquiredInstancesCount, doing so will raise an exception. At the same time, it is always possible to set value of this property to -1.

 

property MaxPoolSize: Int32 read write;

 

Int32 MaxPoolSize { get; set; }

 

Property MaxPoolSize() As Int32

MoveNext

Advances the enumerator to the next element of the collection of non-acquired object pool items.

This property is part of the IEnumerator interface implementation.

 

method MoveNext: Boolean

 

Boolean MoveNext()

 

Function MoveNext() As Boolean

NonAcquiredInstancesCount

Gets the number of pool items that were instantiated but are not currently acquired.

 

property NonAcquiredInstancesCount: Int32 read;

 

Int32 NonAcquiredInstancesCount { get; }

 

ReadOnly Property NonAcquiredInstancesCount() As Int32

OnCreateInstance  protected

Abstract method that is called in the AcquireInstance and Resize methods to instantiate a new object pool item.

 

method OnCreateInstance: TItem

 

TItem OnCreateInstance()

 

Function OnCreateInstance() As TItem

OnDisposeInstance  protected

 

method OnDisposeInstance(instance: TItem)

 

void OnDisposeInstance(TItem instance)

 

Sub OnDisposeInstance(instance As TItem)

Parameters:

  • instance:

PoolSize

Gets the current pool size. The value of this property equals the sum of the AcquiredInstancesCount and the NonAcquiredInstancesCount.

 

property PoolSize: Int32 read;

 

Int32 PoolSize { get; }

 

ReadOnly Property PoolSize() As Int32

ReleaseInstance

Releases an acquired item back into the object pool.

 

method ReleaseInstance(item: TItem)

 

void ReleaseInstance(TItem item)

 

Sub ReleaseInstance(item As TItem)

Parameters:

  • item:

Reset

Sets the enumerator to its initial position, which is before the first element in the collection of non-acquired object pool items.

This property is part of the IEnumerator interface implementation.

 

method Reset

 

void Reset()

 

Sub Reset()

Resize

Instantiates the object pool items until there are at least aNewSize non-acquired items in the pool.

If the value of the second parameter is true, this method also sets the MaxPoolSize to aNewSize.

If the value of the second parameter is false and there are more pool items (both acquired and non-acquired) than aNewSize, an exception is raised.

 

method Resize(newSize: Int32; resetMaxPoolSize: Boolean): Int32

 

Int32 Resize(Int32 newSize, Boolean resetMaxPoolSize)

 

Function Resize(newSize As Int32, resetMaxPoolSize As Boolean) As Int32

Parameters:

  • newSize:
  • resetMaxPoolSize:

WaitTimeOut

Gets or sets the timeout in milliseconds for acquiring new pool items. This is the time that the pool will wait to acquire a new instance before throwing a timeout exception when the Behavior is set to PoolBehavior.Wait.

If the value is -1, the pool will wait indefinitely.

 

property WaitTimeOut: Int32 read write;

 

Int32 WaitTimeOut { get; set; }

 

Property WaitTimeOut() As Int32

 

AcquiredInstancesCount

Gets the count of the currently acquired object pool elements.

 

property AcquiredInstancesCount: Int32 read;

 

Int32 AcquiredInstancesCount { get; }

 

ReadOnly Property AcquiredInstancesCount() As Int32

Behavior

Specifies the behavior of the object pool when an item is requested but the pool is empty (i.e. all available elements are acquired).

See the description of the AcquireInstance method for more details about possible Behavior settings.

 

property Behavior: PoolBehavior read write;

 

PoolBehavior Behavior { get; set; }

 

Property Behavior() As PoolBehavior

Current

Gets the current element in the collection.

This property is a part of the IEnumerator interface implementation.

 

property Current: Object read;

 

Object Current { get; }

 

ReadOnly Property Current() As Object

Instances  protected

Gets all pooled instances as a linked list.

This property can be used in the BaseObjectPool descendants to fine-tune pooled instances etc.

 

property Instances: LinkedList<TItem> read;

 

LinkedList<TItem> Instances { get; }

 

ReadOnly Property Instances() As LinkedList<TItem>

MaxPoolSize

Gets the maximum number of items that can be contained in the pool.

-1 means the pool is of unlimited size.

It is not possible to set this property to a positive value less than the AcquiredInstancesCount, doing so will raise an exception. At the same time, it is always possible to set value of this property to -1.

 

property MaxPoolSize: Int32 read write;

 

Int32 MaxPoolSize { get; set; }

 

Property MaxPoolSize() As Int32

NonAcquiredInstancesCount

Gets the number of pool items that were instantiated but are not currently acquired.

 

property NonAcquiredInstancesCount: Int32 read;

 

Int32 NonAcquiredInstancesCount { get; }

 

ReadOnly Property NonAcquiredInstancesCount() As Int32

PoolSize

Gets the current pool size. The value of this property equals the sum of the AcquiredInstancesCount and the NonAcquiredInstancesCount.

 

property PoolSize: Int32 read;

 

Int32 PoolSize { get; }

 

ReadOnly Property PoolSize() As Int32

WaitTimeOut

Gets or sets the timeout in milliseconds for acquiring new pool items. This is the time that the pool will wait to acquire a new instance before throwing a timeout exception when the Behavior is set to PoolBehavior.Wait.

If the value is -1, the pool will wait indefinitely.

 

property WaitTimeOut: Int32 read write;

 

Int32 WaitTimeOut { get; set; }

 

Property WaitTimeOut() As Int32

 

constructor  protected

Creates a new instance of the BaseObjectPool class.

The created pool will have the size -1 (i.e. infinite).

Cannot be called directly.

 

constructor

 

BaseObjectPool<TItem>()

 

Sub New()

constructor (PoolBehavior, Int32, Int32)  protected

 

constructor(behavior: PoolBehavior; maxSize: Int32; timeout: Int32)

 

BaseObjectPool<TItem>(PoolBehavior behavior, Int32 maxSize, Int32 timeout)

 

Sub New(behavior As PoolBehavior, maxSize As Int32, timeout As Int32)

Parameters:

  • behavior:
  • maxSize:
  • timeout:

AcquireInstance

Acquires an item from the pool.

If there are non-acquired pool items or pool has an infinite size, the pool item is acquired and returned to the caller. Otherwise, the Behavior property specifies the result of the method call:

  • PoolBehavior.Wait - wait until any pool item becomes available.
  • PoolBehavior.IgnoreAndReturn - wait for no more than WaitTimeOut milliseconds for any pool items. If there still are no pool items available after timeout, null will be returned.
  • PoolBehavior.Raise - wait for no more than WaitTimeOut milliseconds for any pool items. If there still are no pool items available after timeout, an NoFreeObjectsInPoolException is raised.

 

method AcquireInstance: TItem

 

TItem AcquireInstance()

 

Function AcquireInstance() As TItem

Clear

Clears all items from the pool.

 

method Clear

 

void Clear()

 

Sub Clear()

Dispose

Releases all pool resources.

The FinalizePool method can be overridden in case some specific cleanup actions are needed.

 

method Dispose

 

void Dispose()

 

Sub Dispose()

DropInstance

Removes the anItem object from the pool.

 

method DropInstance(item: TItem)

 

void DropInstance(TItem item)

 

Sub DropInstance(item As TItem)

Parameters:

  • item:

ExposeNonAcquiredInstances

Exposes all non-acquired pool objects using the specified aDelegate.

 

method ExposeNonAcquiredInstances(action: ExposeNonAcquiredInstancesDelegate<TItem>)

 

void ExposeNonAcquiredInstances(ExposeNonAcquiredInstancesDelegate<TItem> action)

 

Sub ExposeNonAcquiredInstances(action As ExposeNonAcquiredInstancesDelegate<TItem>)

Parameters:

  • action:

FinalizePool  protected

Finalizes the object pool.

This method's implementation is empty in the BaseObjectPool class but can be overriden in derived classes in case some specific cleanup actions are needed.

 

method FinalizePool

 

void FinalizePool()

 

Sub FinalizePool()

GetEnumerator

Returns an enumerator that iterates through a collection of non-acquired pool objects.

This method is part of the IEnumerable interface implementation.

 

method GetEnumerator: IEnumerator

 

IEnumerator GetEnumerator()

 

Function GetEnumerator() As IEnumerator

MoveNext

Advances the enumerator to the next element of the collection of non-acquired object pool items.

This property is part of the IEnumerator interface implementation.

 

method MoveNext: Boolean

 

Boolean MoveNext()

 

Function MoveNext() As Boolean

OnCreateInstance  protected

Abstract method that is called in the AcquireInstance and Resize methods to instantiate a new object pool item.

 

method OnCreateInstance: TItem

 

TItem OnCreateInstance()

 

Function OnCreateInstance() As TItem

OnDisposeInstance  protected

 

method OnDisposeInstance(instance: TItem)

 

void OnDisposeInstance(TItem instance)

 

Sub OnDisposeInstance(instance As TItem)

Parameters:

  • instance:

ReleaseInstance

Releases an acquired item back into the object pool.

 

method ReleaseInstance(item: TItem)

 

void ReleaseInstance(TItem item)

 

Sub ReleaseInstance(item As TItem)

Parameters:

  • item:

Reset

Sets the enumerator to its initial position, which is before the first element in the collection of non-acquired object pool items.

This property is part of the IEnumerator interface implementation.

 

method Reset

 

void Reset()

 

Sub Reset()

Resize

Instantiates the object pool items until there are at least aNewSize non-acquired items in the pool.

If the value of the second parameter is true, this method also sets the MaxPoolSize to aNewSize.

If the value of the second parameter is false and there are more pool items (both acquired and non-acquired) than aNewSize, an exception is raised.

 

method Resize(newSize: Int32; resetMaxPoolSize: Boolean): Int32

 

Int32 Resize(Int32 newSize, Boolean resetMaxPoolSize)

 

Function Resize(newSize As Int32, resetMaxPoolSize As Boolean) As Int32

Parameters:

  • newSize:
  • resetMaxPoolSize:

 

  • CustomObjectPool
  • IObjectPool
  • NoFreeObjectsInPoolException
  • ObjectPool
  • PoolBehavior