1
2
3
4
5 package org.neo.swarm.util.threads;
6
7 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
8
9 /***
10 * Uses Doug Lea's PooledExecutor to handle a threadpool. Pool is highly
11 * configurable through the construction of the PooledExecutor. It handles
12 * blocking thread states and graceful shutdown.
13 *
14 * @author navery
15 */
16 public class OswegoThreadPool implements ThreadPool {
17
18 PooledExecutor pooledExecutor;
19
20 public OswegoThreadPool(PooledExecutor pooledExecutor, int min, int max) {
21 this.pooledExecutor = pooledExecutor;
22 this.pooledExecutor.setMinimumPoolSize(min);
23 this.pooledExecutor.setMaximumPoolSize(max);
24
25 }
26
27
28
29
30
31
32 public void execute(Runnable unitOfWork) throws InterruptedException {
33 this.pooledExecutor.execute(unitOfWork);
34 }
35
36
37
38
39
40
41 public int getMaximumPoolSize() {
42 return pooledExecutor.getMaximumPoolSize();
43 }
44
45
46
47
48
49
50 public int getMinimumPoolSize() {
51 return this.pooledExecutor.getMinimumPoolSize();
52 }
53
54
55
56
57
58
59 public int getPoolSize() {
60 return this.pooledExecutor.getPoolSize();
61 }
62
63
64
65
66
67
68 public void setMaximumPoolSize(int poolSize) {
69 this.pooledExecutor.setMaximumPoolSize(poolSize);
70 }
71
72
73
74
75
76
77 public void setMinimumPoolSize(int poolSize) {
78 this.pooledExecutor.setMinimumPoolSize(poolSize);
79 }
80
81
82
83
84
85
86 public void shutdown(int timeout) {
87 if (timeout == 0) {
88 this.pooledExecutor.shutdownNow();
89 } else if (timeout > 0) {
90 this.pooledExecutor.shutdownAfterProcessingCurrentlyQueuedTasks();
91 try {
92 this.pooledExecutor.awaitTerminationAfterShutdown(timeout);
93 } catch (InterruptedException e) {
94 e.printStackTrace();
95 }
96 }
97 }
98 }