diff --git a/algorithms/data-structures/ObjectPool.cs b/algorithms/data-structures/ObjectPool.cs new file mode 100644 index 0000000..afa5745 --- /dev/null +++ b/algorithms/data-structures/ObjectPool.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Concurrent; + +namespace OctaneDownloadEngine +{ + public class ObjectPool + { + private ConcurrentBag _objects; + private readonly Func _objectGenerator; + + public ObjectPool(Func objectGenerator) + { + _objectGenerator = objectGenerator ?? throw new ArgumentNullException(nameof(objectGenerator)); + _objects = new ConcurrentBag(); + } + + public T Get() => _objects.TryTake(out T item) ? item : _objectGenerator(); + + public void Return(T item) => _objects.Add(item); + + public void Empty() + { + _objects = null; + } + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy