/// <summary> /// Initializes a new pool with specified factory method and maximum size. /// </summary> /// <param name="maximumPoolSize">The maximum pool size limit.</param> /// <param name="factoryMethod">The factory method that will be used to create new objects.</param> public ParameterizedObjectPool(int maximumPoolSize, Func <TKey, TValue> factoryMethod) { // Preconditions if (maximumPoolSize < 1) { throw new ArgumentOutOfRangeException(nameof(maximumPoolSize), ErrorMessages.NegativeOrZeroMaximumPoolSize); } // Assigning properties Diagnostics = new ObjectPoolDiagnostics(); FactoryMethod = factoryMethod; _maximumPoolSize = maximumPoolSize; }
private void InitializePool(int minimumPoolSize, int maximumPoolSize, Func <T> factoryMethod) { // Validating pool limits, exception is thrown if invalid ValidatePoolLimits(minimumPoolSize, maximumPoolSize); // Assigning properties FactoryMethod = factoryMethod; _MaximumPoolSize = maximumPoolSize; _MinimumPoolSize = minimumPoolSize; // Initializing the internal pool data structure PooledObjects = new ConcurrentQueue <T>(); // Creating a new instnce for the Diagnostics class Diagnostics = new ObjectPoolDiagnostics(); // Setting the action for returning to the pool to be integrated in the pooled objects ReturnToPoolAction = ReturnObjectToPool; // Initilizing objects in pool AdjustPoolSizeToBounds(); }