1 16 17 package org.apache.catalina.cluster.tcp; 18 import java.util.List ; 19 import java.util.LinkedList ; 20 21 25 26 public class ThreadPool 27 { 28 33 34 List idle = new LinkedList (); 35 Object mutex = new Object (); 36 Object interestOpsMutex = null; 37 38 ThreadPool (int poolSize, Class threadClass, Object interestOpsMutex) throws Exception { 39 this.interestOpsMutex = interestOpsMutex; 41 for (int i = 0; i < poolSize; i++) { 42 WorkerThread thread = (WorkerThread)threadClass.newInstance(); 43 thread.setPool(this); 44 45 thread.setName (threadClass.getName()+"[" + (i + 1)+"]"); 47 thread.setDaemon(true); 48 thread.setPriority(Thread.MAX_PRIORITY); 49 thread.start(); 50 51 idle.add (thread); 52 } 53 } 54 55 58 WorkerThread getWorker() 59 { 60 WorkerThread worker = null; 61 62 63 synchronized (mutex) { 64 while ( worker == null ) { 65 if (idle.size() > 0) { 66 try { 67 worker = (WorkerThread) idle.remove(0); 68 } catch (java.util.NoSuchElementException x) { 69 worker = null; 71 } 72 } else { 73 try { mutex.wait(); } catch ( java.lang.InterruptedException x ) {} 74 } 75 } 76 } 77 78 return (worker); 79 } 80 81 85 void returnWorker (WorkerThread worker) 86 { 87 synchronized (mutex) { 88 idle.add (worker); 89 mutex.notify(); 90 } 91 } 92 public Object getInterestOpsMutex() { 93 return interestOpsMutex; 94 } 95 } 96 | Popular Tags |