1 45 package org.exolab.jms.threads; 46 47 import java.util.HashMap ; 48 49 import org.exolab.jms.service.BasicService; 50 import org.exolab.jms.service.ServiceException; 51 import org.exolab.jms.service.ServiceState; 52 import org.exolab.jms.common.threads.ThreadPool; 53 54 55 75 public class ThreadPoolManager extends BasicService { 76 77 private static HashMap _pools = null; 79 80 private static ThreadPoolManager _instance = null; 82 83 private static final String THREADPOOLMGR_NAME = "ThreadPoolManager"; 85 86 87 94 private ThreadPoolManager() { 95 super(THREADPOOLMGR_NAME); 96 _pools = new HashMap (); 97 } 98 99 105 static public ThreadPoolManager instance() { 106 if (_instance == null) { 107 _instance = new ThreadPoolManager(); 108 } 109 return _instance; 110 } 111 112 122 public ThreadPool createThreadPool(String name, int size) 123 throws ThreadPoolExistsException { 124 synchronized (_pools) { 125 if (_pools.containsKey(name)) { 126 throw new ThreadPoolExistsException("ThreadPool with name " + 127 name + " exists"); 128 } 129 ThreadPool pool = new ThreadPool(name, size, true); 130 131 _pools.put(name, pool); 132 return pool; 133 } 134 } 135 136 144 public ThreadPool getThreadPool(String name) 145 throws UnknownThreadPoolException { 146 ThreadPool pool = null; 147 148 synchronized (_pools) { 149 pool = (ThreadPool) _pools.get(name); 150 } 151 152 if (pool == null) { 153 throw new UnknownThreadPoolException("ThreadPool with name " + 154 name + " does not exist"); 155 } 156 157 return pool; 158 } 159 160 168 public void deleteThreadPool(String name) 169 throws UnknownThreadPoolException { 170 ThreadPool pool = null; 171 172 synchronized (_pools) { 173 pool = (ThreadPool) _pools.remove(name); 174 } 175 176 if (pool == null) { 177 throw new UnknownThreadPoolException("ThreadPool with name " + 178 name + " does not exist"); 179 } 180 pool.stopRequestAllWorkers(); 181 } 182 183 186 public synchronized void run() { 187 while (getState() != ServiceState.STOPPED) { 188 try { 189 wait(); 191 } catch (InterruptedException ignore) { 192 } 193 } 194 } 195 196 202 public void stop() throws ServiceException { 203 synchronized (_pools) { 204 Object [] ob = _pools.values().toArray(); 205 206 for (int i = 0, j = _pools.size(); i < j; i++) { 207 ((ThreadPool) ob[i]).stopRequestAllWorkers(); 208 } 209 _pools.clear(); 210 } 211 super.stop(); 212 _pools = null; 214 _instance = null; 215 } 216 217 } | Popular Tags |