Skip to content

Commit 2883f9c

Browse files
committed
优化对象池结构
1 parent 10d4f0b commit 2883f9c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ObjectPool.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ namespace KBEngine
1313
/// <typeparam name="T">对象类型</typeparam>
1414
public class ObjectPool<T> where T : new()
1515
{
16-
private static LinkedList<T> _objects = new LinkedList<T>();
17-
16+
private static Stack<T> _objects = new Stack<T>();
17+
private static T v;
18+
1819
public static T createObject()
1920
{
2021
lock (_objects)
2122
{
22-
if (_objects.First != null)
23+
if (_objects.Count > 0)
2324
{
24-
T v = _objects.First.Value;
25-
_objects.RemoveFirst();
25+
v = _objects.Pop();
2626
return v;
2727
}
2828
else
@@ -36,7 +36,7 @@ public static void reclaimObject(T item)
3636
{
3737
lock (_objects)
3838
{
39-
_objects.AddLast(item);
39+
_objects.Push(item);
4040
}
4141
}
4242
}

0 commit comments

Comments
 (0)