-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kbengine
committed
Apr 9, 2016
1 parent
1ff2560
commit 53d0be4
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using UnityEngine; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
|
||
namespace KBEngine | ||
{ | ||
/// <summary> | ||
/// 简单的对象池 | ||
/// </summary> | ||
/// <typeparam name="T">对象类型</typeparam> | ||
public class ObjectPool<T> where T : new() | ||
{ | ||
private static LinkedList<T> _objects = new LinkedList<T>(); | ||
|
||
public static T getObject() | ||
{ | ||
lock (_objects) | ||
{ | ||
if (_objects.First != null) | ||
{ | ||
T v = _objects.First.Value; | ||
_objects.RemoveFirst(); | ||
return v; | ||
} | ||
else | ||
{ | ||
return new T(); | ||
} | ||
} | ||
} | ||
|
||
public static void putObject(T item) | ||
{ | ||
lock (_objects) | ||
{ | ||
_objects.AddLast(item); | ||
} | ||
} | ||
} | ||
|
||
} |