1 package org.apache.axis2.util.threadpool; 2 3 import org.apache.axis2.engine.AxisFault; 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 17 18 public class ThreadPool { 19 20 protected static Log log = LogFactory.getLog(ThreadPool.class.getName()); 21 22 private static int MAX_THREAD_COUNT = 10; 23 protected static long SLEEP_INTERVAL = 1000; 24 private static List threads; 25 private static List tasks; 26 private static boolean shoutDown; 27 28 public ThreadPool() { 29 threads = new ArrayList (); 30 tasks = new ArrayList (); 31 32 for (int i = 0; i < MAX_THREAD_COUNT; i++) { 33 ThreadWorker threadWorker = new ThreadWorker(); 34 threadWorker.setPool(this); 35 threads.add(threadWorker); 36 threadWorker.start(); 37 } 38 39 } 40 41 public void addWorker(AxisWorker worker) throws AxisFault { 42 if (shoutDown) 43 throw new AxisFault("Thread Pool is Shutting Down"); 44 tasks.add(worker); 45 } 46 47 public synchronized AxisWorker getWorker() { 48 if (!tasks.isEmpty()) { 49 AxisWorker worker = (AxisWorker) tasks.get(0); 50 tasks.remove(worker); 51 return worker; 52 } else 53 return null; 54 } 55 56 63 public void safeShutDown() throws AxisFault { 64 synchronized (this) { 65 shoutDown = true; 66 } 67 while (!tasks.isEmpty()) { 68 try { 69 Thread.sleep(SLEEP_INTERVAL); 70 } catch (InterruptedException e) { 71 throw new AxisFault("Error while safeShutDown", e); 72 } 73 } 74 forceShutDown(); 75 76 } 77 78 81 public void forceShutDown() { 82 if (log.isDebugEnabled()) 83 log.debug("forceShutDown called. Thread workers will be stopped"); 84 Iterator ite = threads.iterator(); 85 while (ite.hasNext()) { 86 ThreadWorker worker = (ThreadWorker) ite.next(); 87 worker.setStop(true); 88 } 89 } 90 } 91 | Popular Tags |