View Javadoc

1   /*
2    * Created on May 19, 2004
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  //		this.pooledExecutor.setKeepAliveTime(arg0);
25  	}
26  
27  	/*
28  	 * (non-Javadoc)
29  	 * 
30  	 * @see org.neo.swarm.util.threads.ThreadPool#execute(java.lang.Runnable)
31  	 */
32  	public void execute(Runnable unitOfWork) throws InterruptedException {
33  		this.pooledExecutor.execute(unitOfWork);
34  	}
35  	
36  	/*
37  	 * (non-Javadoc)
38  	 * 
39  	 * @see org.neo.swarm.util.threads.ThreadPool#getMaximumPoolSize()
40  	 */
41  	public int getMaximumPoolSize() {
42  		return pooledExecutor.getMaximumPoolSize();
43  	}
44  
45  	/*
46  	 * (non-Javadoc)
47  	 * 
48  	 * @see org.neo.swarm.util.threads.ThreadPool#getMinimumPoolSize()
49  	 */
50  	public int getMinimumPoolSize() {
51  		return this.pooledExecutor.getMinimumPoolSize();
52  	}
53  	
54  	/*
55  	 * (non-Javadoc)
56  	 * 
57  	 * @see org.neo.swarm.util.threads.ThreadPool#getPoolSize()
58  	 */
59  	public int getPoolSize() {
60  		return this.pooledExecutor.getPoolSize();
61  	}
62  	
63  	/*
64  	 * (non-Javadoc)
65  	 * 
66  	 * @see org.neo.swarm.util.threads.ThreadPool#setMaximumPoolSize(int)
67  	 */
68  	public void setMaximumPoolSize(int poolSize) {
69  		this.pooledExecutor.setMaximumPoolSize(poolSize);
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * 
75  	 * @see org.neo.swarm.util.threads.ThreadPool#setMinimumPoolSize(int)
76  	 */
77  	public void setMinimumPoolSize(int poolSize) {
78  		this.pooledExecutor.setMinimumPoolSize(poolSize);
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * 
84  	 * @see org.neo.swarm.util.threads.ThreadPool#shutdown(int)
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  }