Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kbengine committed Apr 9, 2016
1 parent 1ff2560 commit 53d0be4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ObjectPool.cs
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);
}
}
}

}

0 comments on commit 53d0be4

Please sign in to comment.