private void Return(SingelObject <T> poolObject) { lock (LockObj) { used.Remove(poolObject); /* 每次都放在最前面,这样若是需要的连接数比较少, * 而创建的连接数又比较多的话,会导致队列尾的若干个连接一直不会被用到 * 从而可以被检查线程自动释放掉 */ list.Insert(0, poolObject); } }
private SingelObject <T> Get() { lock (LockObj) { if (this._MaxCount > 0 && this.CurrentCount >= this._MaxCount) { return(null); } int sum = 0; while (sum++ <= 5) { SingelObject <T> st = null; if (list.Count > 0) { st = list.First(); list.RemoveAt(0); } else { var t = _Pobject.Create(); st = new SingelObject <T>() { Obj = t }; } if (st.Obj != default(T) && _Pobject.CanUse(st.Obj)) { st.LastUseTime = DateTime.Now; used.Add(st); return(st); } else { list.Remove(st); } System.Threading.Thread.Sleep(200); } return(null); } }
public bool Run(Action <T> act) { SingelObject <T> st = null; st = this.Get(); T t = default(T); if (st == null) { return(false); } try { t = st.Obj; act.Invoke(t); } catch { } finally { this.Return(st); } return(true); }