ITeye博客

A simple thread pool implementation

willpower · 2007-03-07 11:53 · Study 学习 · Markdown

java 代码

  1. import java.util.Vector;
  2. /**
  3. * Thread pool
  4. */
  5. public class ThreadPool implements Runnable {
  6. // Default ThreadPool minimum size
  7. public final static int DEFAULT_MIN_SIZE = 0;
  8. // Default ThreadPool maximum size
  9. public final static int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
  10. public final static long DEFAULT_RELEASE_DELAY = 10 * 1000;
  11. // customized thread pool minimum size
  12. protected int minSize;
  13. // customized thread pool maximum size
  14. protected int maxSize;
  15. protected long releaseDelay;
  16. // current threads size in threadpool
  17. protected int currentSize;
  18. protected int availableThreads;
  19. // task list
  20. protected Vector taskList;
  21. /**
  22. * customized ThradPool
  23. *
  24. * @param minSize
  25. *            minimum thread pool size
  26. * @param maxSize
  27. *            maximum thread pool size
  28. * @param releaseDelay
  29. *            threads release delay
  30. */
  31. public ThreadPool(int minSize, int maxSize, long releaseDelay) {
  32. this.minSize = minSize;
  33. this.maxSize = maxSize;
  34. this.releaseDelay = releaseDelay;
  35. taskList = new Vector(100);
  36. availableThreads = 0;
  37. }
  38. /**
  39. * Default ThreadPool
  40. */
  41. public ThreadPool() {
  42. this(DEFAULT_MIN_SIZE, DEFAULT_MIN_SIZE, DEFAULT_RELEASE_DELAY);
  43. }
  44. /**
  45. * set minimum thread pool size
  46. *
  47. * @param minSize
  48. *            minimum thread pool size
  49. */
  50. public synchronized void setMinSize(int minSize) {
  51. this.minSize = minSize;
  52. }
  53. /**
  54. * get minimum thread pool size
  55. */
  56. public synchronized int getMinSize() {
  57. return minSize;
  58. }
  59. /**
  60. * set maximum thread pool size
  61. *
  62. * @param maxSize
  63. *            maximum thread pool size
  64. */
  65. public synchronized void setMaxSize(int maxSize) {
  66. this.maxSize = maxSize;
  67. }
  68. /**
  69. * get maximum thread pool size
  70. */
  71. public synchronized int getMaxSize() {
  72. return maxSize;
  73. }
  74. /**
  75. * set thread release delay
  76. *
  77. * @param releaseDelay
  78. *            thread release delay time
  79. */
  80. public synchronized void setReleaseDelay(long releaseDelay) {
  81. this.releaseDelay = releaseDelay;
  82. }
  83. /**
  84. * get thread release delay
  85. */
  86. public synchronized long getReleaseDelay() {
  87. return releaseDelay;
  88. }
  89. /**
  90. * add a task to task list of ThreadPool
  91. *
  92. * @param runnable
  93. *            new task
  94. */
  95. public synchronized void addTask(Runnable runnable) {
  96. taskList.addElement(runnable);
  97. if (availableThreads > 0) {
  98. this.notify();
  99. } else {
  100. if (currentSize < maxSize) {
  101. Thread t = new Thread(this);
  102. currentSize++;
  103. t.start();
  104. }
  105. }
  106. }
  107. public void run() {
  108. Runnable task;
  109. while (true) {
  110. synchronized (this) {
  111. if (currentSize > maxSize) {
  112. currentSize--;
  113. break;
  114. }
  115. task = getNextTask();
  116. if (task == null) {
  117. try {
  118. availableThreads++;
  119. wait(releaseDelay);
  120. availableThreads--;
  121. } catch (InterruptedException ie) {
  122. // do something you wanna
  123. }
  124. task = getNextTask();
  125. if (task == null) {
  126. if (currentSize < minSize)
  127. continue;
  128. currentSize--;
  129. break;
  130. }
  131. }
  132. }
  133. try {
  134. task.run();
  135. } catch (Exception e) {
  136. System.err.println("Uncaught exception");
  137. e.printStackTrace(System.err);
  138. }
  139. }
  140. }
  141. /**
  142. * get the next task from task list.
  143. *
  144. */
  145. protected synchronized Runnable getNextTask() {
  146. Runnable task = null;
  147. if (taskList.size() > 0) {
  148. task = (Runnable) (taskList.elementAt(0));
  149. taskList.removeElementAt(0);
  150. }
  151. return task;
  152. }
  153. /**
  154. * return thread pool message
  155. */
  156. public String toString() {
  157. StringBuffer sb = new StringBuffer();
  158. sb.append("DEFAULT_MIN_SIZE : " + DEFAULT_MIN_SIZE + "\n");
  159. sb.append("DEFAULT_MAX_SIZE : " + DEFAULT_MAX_SIZE + "\n");
  160. sb.append("DEFAULT_RELEASE_DELAY : " + DEFAULT_RELEASE_DELAY + "\n");
  161. sb.append("the information about your's construct ThreadPool below : \n");
  162. sb.append("minSize \t maxSize \t releaseDelay \n");
  163. sb.append(minSize + "\t" + maxSize + "\t" + releaseDelay);
  164. return sb.toString();
  165. }
  166. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。