ThreadPoolExecutor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using ST = System.Threading;
  4. namespace SharpCifs.Util.Sharpen
  5. {
  6. class ThreadPoolExecutor
  7. {
  8. ThreadFactory _tf;
  9. int _corePoolSize;
  10. int _maxPoolSize;
  11. List<Thread> _pool = new List<Thread>();
  12. int _runningThreads;
  13. int _freeThreads;
  14. bool _shutdown;
  15. Queue<IRunnable> _pendingTasks = new Queue<IRunnable>();
  16. public ThreadPoolExecutor(int corePoolSize, ThreadFactory factory)
  17. {
  18. this._corePoolSize = corePoolSize;
  19. _maxPoolSize = corePoolSize;
  20. _tf = factory;
  21. }
  22. public void SetMaximumPoolSize(int size)
  23. {
  24. _maxPoolSize = size;
  25. }
  26. public bool IsShutdown()
  27. {
  28. return _shutdown;
  29. }
  30. public virtual bool IsTerminated()
  31. {
  32. lock (_pendingTasks)
  33. {
  34. return _shutdown && _pendingTasks.Count == 0;
  35. }
  36. }
  37. public virtual bool IsTerminating()
  38. {
  39. lock (_pendingTasks)
  40. {
  41. return _shutdown && !IsTerminated();
  42. }
  43. }
  44. public int GetCorePoolSize()
  45. {
  46. return _corePoolSize;
  47. }
  48. public void PrestartAllCoreThreads()
  49. {
  50. lock (_pendingTasks)
  51. {
  52. while (_runningThreads < _corePoolSize)
  53. StartPoolThread();
  54. }
  55. }
  56. public void SetThreadFactory(ThreadFactory f)
  57. {
  58. _tf = f;
  59. }
  60. public void Execute(IRunnable r)
  61. {
  62. InternalExecute(r, true);
  63. }
  64. internal void InternalExecute(IRunnable r, bool checkShutdown)
  65. {
  66. lock (_pendingTasks)
  67. {
  68. if (_shutdown && checkShutdown)
  69. throw new InvalidOperationException();
  70. if (_runningThreads < _corePoolSize)
  71. {
  72. StartPoolThread();
  73. }
  74. else if (_freeThreads > 0)
  75. {
  76. _freeThreads--;
  77. }
  78. else if (_runningThreads < _maxPoolSize)
  79. {
  80. StartPoolThread();
  81. }
  82. _pendingTasks.Enqueue(r);
  83. ST.Monitor.PulseAll(_pendingTasks);
  84. }
  85. }
  86. void StartPoolThread()
  87. {
  88. _runningThreads++;
  89. _pool.Add(_tf.NewThread(new RunnableAction(RunPoolThread)));
  90. }
  91. public void RunPoolThread()
  92. {
  93. while (!IsTerminated())
  94. {
  95. try
  96. {
  97. IRunnable r = null;
  98. lock (_pendingTasks)
  99. {
  100. _freeThreads++;
  101. while (!IsTerminated() && _pendingTasks.Count == 0)
  102. ST.Monitor.Wait(_pendingTasks);
  103. if (IsTerminated())
  104. break;
  105. r = _pendingTasks.Dequeue();
  106. }
  107. if (r != null)
  108. r.Run();
  109. }
  110. //supress all errors, anyway
  111. //catch (ST.ThreadAbortException) {
  112. // // Do not catch a thread abort. If we've been aborted just let the thread die.
  113. // // Currently reseting an abort which was issued because the appdomain is being
  114. // // torn down results in the process living forever and consuming 100% cpu time.
  115. // return;
  116. //}
  117. catch
  118. {
  119. }
  120. }
  121. }
  122. public virtual void Shutdown()
  123. {
  124. lock (_pendingTasks)
  125. {
  126. _shutdown = true;
  127. ST.Monitor.PulseAll(_pendingTasks);
  128. }
  129. }
  130. public virtual List<IRunnable> ShutdownNow()
  131. {
  132. lock (_pendingTasks)
  133. {
  134. _shutdown = true;
  135. foreach (var t in _pool)
  136. {
  137. try
  138. {
  139. t.Cancel(true);
  140. t.Dispose();
  141. }
  142. catch { }
  143. }
  144. _pool.Clear();
  145. _freeThreads = 0;
  146. _runningThreads = 0;
  147. var res = new List<IRunnable>(_pendingTasks);
  148. _pendingTasks.Clear();
  149. return res;
  150. }
  151. }
  152. }
  153. class RunnableAction : IRunnable
  154. {
  155. Action _action;
  156. public RunnableAction(Action a)
  157. {
  158. _action = a;
  159. }
  160. public void Run()
  161. {
  162. _action();
  163. }
  164. }
  165. }