1 8 package examples.util.concurrent; 9 10 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 11 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 12 import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer; 13 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 14 import examples.util.definition.Definition; 15 16 21 public class AsynchronousManager { 22 23 protected static final AsynchronousManager INSTANCE = new AsynchronousManager(); 24 25 protected PooledExecutor m_threadPool = null; 26 27 protected boolean m_initialized = false; 28 29 34 public void execute(final Runnable task) { 35 if (notInitialized()) { 36 throw new IllegalStateException ("asynchronous thread pool not initialized"); 37 } 38 try { 39 m_threadPool.execute(task); 40 } catch (InterruptedException e) { 41 Thread.currentThread().interrupt(); 42 notifyAll(); 43 throw new WrappedRuntimeException(e); 44 } catch (Exception e) { 45 throw new WrappedRuntimeException(e); 46 } 47 } 48 49 54 public static AsynchronousManager getInstance() { 55 return INSTANCE; 56 } 57 58 63 public synchronized void initialize(final Definition definition) { 64 if (definition == null) { 65 return; 66 } 67 if (m_initialized) { 68 return; 69 } 70 examples.util.definition.ThreadPoolDefinition def = (examples.util.definition.ThreadPoolDefinition) definition; 71 int threadPoolMaxSize = def.getMaxSize(); 72 int threadPoolInitSize = def.getInitSize(); 73 int threadPoolMinSize = def.getMinSize(); 74 int keepAliveTime = def.getKeepAliveTime(); 75 boolean waitWhenBlocked = def.getWaitWhenBlocked(); 76 boolean bounded = def.getBounded(); 77 if (threadPoolMaxSize < threadPoolInitSize || threadPoolMaxSize < threadPoolMinSize) { 78 throw new IllegalArgumentException ("max size of thread pool can not exceed the init size"); 79 } 80 81 if (bounded) { 84 createBoundedThreadPool( 85 threadPoolMaxSize, 86 threadPoolMinSize, 87 threadPoolInitSize, 88 keepAliveTime, 89 waitWhenBlocked 90 ); 91 } else { 92 createDynamicThreadPool(threadPoolMinSize, threadPoolInitSize, keepAliveTime); 93 } 94 m_initialized = true; 95 } 96 97 100 public void stop() { 101 m_threadPool.shutdownNow(); 102 } 103 104 113 protected void createBoundedThreadPool(final int threadPoolMaxSize, 114 final int threadPoolMinSize, 115 final int threadPoolInitSize, 116 final int keepAliveTime, 117 final boolean waitWhenBlocked) { 118 m_threadPool = new PooledExecutor(new BoundedBuffer(threadPoolInitSize), threadPoolMaxSize); 119 m_threadPool.setKeepAliveTime(keepAliveTime); 120 m_threadPool.createThreads(threadPoolInitSize); 121 m_threadPool.setMinimumPoolSize(threadPoolMinSize); 122 if (waitWhenBlocked) { 123 m_threadPool.waitWhenBlocked(); 124 } 125 } 126 127 134 protected void createDynamicThreadPool(final int threadPoolMinSize, 135 final int threadPoolInitSize, 136 final int keepAliveTime) { 137 m_threadPool = new PooledExecutor(new LinkedQueue()); 138 m_threadPool.setKeepAliveTime(keepAliveTime); 139 m_threadPool.createThreads(threadPoolInitSize); 140 m_threadPool.setMinimumPoolSize(threadPoolMinSize); 141 } 142 143 148 protected boolean notInitialized() { 149 return !m_initialized; 150 } 151 152 155 protected AsynchronousManager() { 156 } 157 } | Popular Tags |