CombatPool.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. namespace CombatLibrary.CombatLibrary.CombatCore
  4. {
  5. public class CombatPool<T> where T : new()
  6. {
  7. private List<T> allPool = new List<T>();
  8. public int count = 1000;
  9. private HashSet<int> _map = new HashSet<int>();
  10. public T Get()
  11. {
  12. // lock (allPool)
  13. {
  14. if (allPool.Count > 0)
  15. {
  16. T betterList = allPool[0];
  17. allPool.RemoveAt(0);
  18. // System.GC.ReRegisterForFinalize(betterList);
  19. int code = betterList.GetHashCode();
  20. _map.Remove(code);
  21. return betterList;
  22. }
  23. else
  24. {
  25. T betterList = new T();
  26. return betterList;
  27. }
  28. }
  29. }
  30. public void Clear()
  31. {
  32. allPool.Clear();
  33. _map.Clear();
  34. }
  35. public void Recycle(T a)
  36. {
  37. if (a != null)
  38. {
  39. // lock (allPool)
  40. {
  41. int code = a.GetHashCode();
  42. if (_map.Count > 1000)
  43. {
  44. return;
  45. }
  46. if (!_map.Contains(code))
  47. {
  48. // System.GC.SuppressFinalize(a);
  49. allPool.Add(a);
  50. _map.Add(code);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }