ThreadPool.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using CommonLang.Log;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. namespace CommonLang.Concurrent
  9. {
  10. public class ThreadExecutor : Executor
  11. {
  12. private AtomicLong idgen = new AtomicLong(0);
  13. private Logger log;
  14. private Thread[] processors;
  15. private Queue<WorkTask> workQueue = new Queue<WorkTask>();
  16. private bool shutdown = false;
  17. public ThreadExecutor(int processorCount)
  18. {
  19. this.log = LoggerFactory.GetLogger(string.Format("ThreadExecutor"));
  20. this.processors = new Thread[processorCount];
  21. for (int i = 0; i < processorCount; i++)
  22. {
  23. this.processors[i] = new Thread(internal_run);
  24. this.processors[i].IsBackground = true;
  25. this.processors[i].Start();
  26. }
  27. }
  28. public void Shutdown()
  29. {
  30. log.Debug("processors shutting down ...");
  31. shutdown = true;
  32. lock (workQueue)
  33. {
  34. foreach (WorkTask task in workQueue)
  35. {
  36. task.Cancel();
  37. }
  38. }
  39. for (int i = 0; i < processors.Length; i++)
  40. {
  41. this.processors[i].Join();
  42. }
  43. log.Debug("processors shutdown !");
  44. }
  45. private void internal_run()
  46. {
  47. try
  48. {
  49. while (!shutdown)
  50. {
  51. WorkTask task = null;
  52. int taskCount;
  53. lock (workQueue)
  54. {
  55. taskCount = workQueue.Count;
  56. if (taskCount > 0)
  57. {
  58. task = workQueue.Dequeue();
  59. }
  60. else
  61. {
  62. Monitor.Wait(workQueue);
  63. continue;
  64. }
  65. }
  66. try
  67. {
  68. task.Run();
  69. }
  70. catch (Exception err)
  71. {
  72. log.Error(err.Message, err);
  73. }
  74. }
  75. }
  76. catch (Exception err)
  77. {
  78. log.Error(err.Message, err);
  79. }
  80. }
  81. private void internal_queue_task(WorkTask task)
  82. {
  83. lock (workQueue)
  84. {
  85. workQueue.Enqueue(task);
  86. Monitor.Pulse(workQueue);
  87. }
  88. }
  89. public Future Execute(Action command)
  90. {
  91. WorkTask task = new WorkTask(this, command);
  92. internal_queue_task(task);
  93. return task;
  94. }
  95. public Future Schedule(Action r, int delay)
  96. {
  97. return new ScheduledWorkTask(this, r, delay, System.Threading.Timeout.Infinite);
  98. }
  99. public Future ScheduleAtFixedRate(Action r, int initial, int period)
  100. {
  101. return new ScheduledWorkTask(this, r, initial, period);
  102. }
  103. private class WorkTask : Future
  104. {
  105. internal readonly ThreadExecutor exec;
  106. internal readonly long id;
  107. internal readonly Action action;
  108. internal bool is_cancel = false;
  109. public WorkTask(ThreadExecutor exec, Action action)
  110. {
  111. this.exec = exec;
  112. this.id = exec.idgen.IncrementAndGet();
  113. this.action = action;
  114. }
  115. public bool Cancel()
  116. {
  117. lock (this)
  118. {
  119. this.is_cancel = true;
  120. }
  121. return is_cancel;
  122. }
  123. public long ID { get { return id; } }
  124. public bool IsCancelled { get { lock (this) { return is_cancel; } } }
  125. internal virtual void Run()
  126. {
  127. lock (this)
  128. {
  129. if (is_cancel) { return; }
  130. }
  131. action();
  132. }
  133. }
  134. private class ScheduledWorkTask : WorkTask
  135. {
  136. internal readonly int initial;
  137. internal readonly int period;
  138. internal Timer timer;
  139. public ScheduledWorkTask(ThreadExecutor exec, Action command, int initial, int period)
  140. : base(exec, command)
  141. {
  142. this.initial = initial;
  143. this.period = period;
  144. this.timer = new Timer(timeout, command, initial, period);
  145. }
  146. private void timeout(object state)
  147. {
  148. lock (this)
  149. {
  150. if (is_cancel)
  151. {
  152. this.timer.Dispose();
  153. return;
  154. }
  155. if (period == Timeout.Infinite)
  156. {
  157. this.timer.Dispose();
  158. }
  159. }
  160. exec.internal_queue_task(this);
  161. }
  162. }
  163. }
  164. public class SingleThreadExecutor : ThreadExecutor
  165. {
  166. public SingleThreadExecutor() : base(1)
  167. {
  168. }
  169. }
  170. }