浅谈Pool对象
发布时间:2016-06-05 10:00:06
Pool对象的技术指标:避免频繁创建经常使用的稀有资源,提高工作效率.控制阀值,很多情况下一些关键资源都有一个最佳并发数,超过这个拐点性能有可能急剧下降,也有可能继续增大并发数性能不能提升.安全的获取对象和释放对象(使用之后放回连接池)public sealed class Po ...
Pool对象的技术指标:
避免频繁创建经常使用的稀有资源,提高工作效率.
控制阀值,很多情况下一些关键资源都有一个最佳并发数,超过这个拐点性能有可能急剧下降,也有可能继续增大并发数性能不能提升.
安全的获取对象和释放对象(使用之后放回连接池)
public sealed class Pool<T> : IDisposable where T : IDisposable { private bool disposed = false; private Semaphore gate; private Stack<T> pool; public event Predicate<T> Predicate; public Pool(int concrrent,Func<Pool<T>,T> activator) { if (concrrent <= 0) { throw new ArgumentException("concrrent"); } if (activator==null) { throw new ArgumentNullException("activator"); } gate = new Semaphore(concrrent, concrrent); pool = new Stack<T>(); for (int i=0;i< concrrent; i++) { pool.Push(activator(this)); } } public T Acquire() { if (!gate.WaitOne()) throw new InvalidOperationException(); lock (pool) { return pool.Pop(); } } public void Release(T target) { lock (pool) { if (Predicate!=null) { if (Predicate(target)) { pool.Push(target); gate.Release(); } } else { if (target != null) { pool.Push(target); gate.Release(); } } } } private void Dispose(bool disposing) { if (disposed) return; if (disposing) { gate.Dispose(); } for (int i = 0; i < pool.Count; i++) { var t = pool.Pop(); t.Dispose(); } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Pool() { Dispose(false); } }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:浅谈Pool对象
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。
临沂海外仓:https://www.goluckyvip.com/tag/95388.html
国外仓储服务:https://www.goluckyvip.com/tag/95389.html
上海 海外仓:https://www.goluckyvip.com/tag/95393.html
中国海外仓库:https://www.goluckyvip.com/tag/95395.html
北京市海外仓:https://www.goluckyvip.com/tag/95398.html
ecowas:https://www.goluckyvip.com/tag/954.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html