C# 泛型 池

工作需要,写个泛型池,线程安全,分享一下。

 

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 
  5 namespace Ch.Common
  6 {
  7     public class Pool<T> where T: class,new()
  8     {
  9         /// <summary>
 10         /// 对象容器
 11         /// </summary>
 12         protected T[] Container;
 13         /// <summary>
 14         /// 同步锁
 15         /// </summary>
 16         private readonly object _lock;
 17         /// <summary>
 18         /// 空地址清单
 19         /// </summary>
 20         private readonly Queue<int> _empties;
 21         /// <summary>
 22         /// 对象清单
 23         /// </summary>
 24         private readonly Queue<int> _indexs;
 25         /// <summary>
 26         /// 最大保留对象数量
 27         /// </summary>
 28         public int Max { get; set; }
 29         public Pool()
 30             : this(4)
 31         {
 32 
 33         }
 34         /// <summary>
 35         /// 生成指定数量存储空间池
 36         /// </summary>
 37         /// <param name="capacity">存储空间数量</param>
 38         public Pool(int capacity)
 39         {
 40             Max = int.MaxValue;
 41             _lock = new object();
 42             Container = new T[capacity];
 43             _indexs = new Queue<int>(capacity);
 44             _empties = new Queue<int>(capacity);
 45             for (var i = 0; i < capacity; i++)
 46             {
 47                 _empties.Enqueue(i);
 48             }
 49         }
 50 
 51         /// <summary>
 52         /// 获得对象
 53         /// </summary>
 54         /// <param name="item">对象</param>
 55         /// <returns>对象编号</returns>
 56         public int Get(out T item)
 57         {
 58             lock (_lock)
 59             {
 60                 //有待命对象直接返回
 61                 if (_indexs.Count>0)
 62                 {
 63                     var index = _indexs.Dequeue();
 64                     item = Container[index];
 65                     return index;
 66                 }
 67                 //有空余空间
 68                 //生成对象返回
 69                 if (_empties.Count>0)
 70                 {
 71                     var index = _empties.Dequeue();
 72                     Container[index] = new T();
 73                     item = Container[index];
 74                     return index;
 75                 }
 76                 //申请新空间,返回对象
 77                 var size = Container.Count();
 78                 var temp = new T[size*2];
 79                 Array.Copy(Container, temp, size);
 80                 Array.Clear(Container, 0, size);
 81                 Container = temp;
 82                 Container[size]=new T();
 83                 item = Container[size];
 84                 for (var i = size + 1; i < size * 2; i++)
 85                 {
 86                     _empties.Enqueue(i);
 87                 }
 88                 return size;
 89             }
 90         }
 91         /// <summary>
 92         /// 释放对象
 93         /// </summary>
 94         /// <param name="index">对象序列号</param>
 95         public void Free(int index)
 96         {
 97             lock (_lock)
 98             {
 99                 //当前池中超过最大对象数量
100                 //直接释放
101                 if (_indexs.Count>Max)
102                 {
103                     _empties.Enqueue(index);
104                     var temp = Container[index] as IDisposable;
105                     if (temp != null)
106                     {
107                         temp.Dispose();
108                     }
109                     Container[index] = null;
110                 }
111                 else
112                 {
113                     _indexs.Enqueue(index);
114                 }
115             }
116         }
117     }
118 }

 

获取对象。。

获取对象
1 var pool = new Pool<User>();
2 User u;
3 var index = pool.Get(out u);

 

设置最大对象数量。。

View Code
1             var pool = new Pool<User>();
2             //最大池中数量 8
3             pool.Max = 8;
4             User u;
5             var index = pool.Get(out u);

 

释放对象。。

释放对象
1             var pool = new Pool<User>();
2             //最大池中数量 8
3             pool.Max = 8;
4             User u;
5             var index = pool.Get(out u);
6 
7             //释放对象
8             pool.Free(index);

posted on 2012-08-18 15:36  亚尔斯兰  阅读(157)  评论(0)    收藏  举报

导航