Skip to content

Commit 53d0be4

Browse files
author
kbengine
committed
up
1 parent 1ff2560 commit 53d0be4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

ObjectPool.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using UnityEngine;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
7+
8+
namespace KBEngine
9+
{
10+
/// <summary>
11+
/// 简单的对象池
12+
/// </summary>
13+
/// <typeparam name="T">对象类型</typeparam>
14+
public class ObjectPool<T> where T : new()
15+
{
16+
private static LinkedList<T> _objects = new LinkedList<T>();
17+
18+
public static T getObject()
19+
{
20+
lock (_objects)
21+
{
22+
if (_objects.First != null)
23+
{
24+
T v = _objects.First.Value;
25+
_objects.RemoveFirst();
26+
return v;
27+
}
28+
else
29+
{
30+
return new T();
31+
}
32+
}
33+
}
34+
35+
public static void putObject(T item)
36+
{
37+
lock (_objects)
38+
{
39+
_objects.AddLast(item);
40+
}
41+
}
42+
}
43+
44+
}

0 commit comments

Comments
 (0)