1 17 package org.apache.excalibur.thread.impl; 18 19 import org.apache.excalibur.thread.Executable; 20 import org.apache.excalibur.thread.ThreadControl; 21 import org.apache.excalibur.thread.ThreadPool; 22 23 30 public abstract class AbstractThreadPool 31 implements ThreadPool 32 { 33 36 private final ThreadGroup m_threadGroup; 37 38 42 private final String m_name; 43 44 49 private int m_level; 50 51 58 public AbstractThreadPool( final String name, 59 final ThreadGroup threadGroup ) 60 throws Exception 61 { 62 if( null == name ) 63 { 64 throw new NullPointerException ( "name" ); 65 } 66 if( null == threadGroup ) 67 { 68 throw new NullPointerException ( "threadGroup" ); 69 } 70 71 m_name = name; 72 m_threadGroup = threadGroup; 73 } 74 75 80 protected void destroyWorker( final WorkerThread thread ) 81 { 82 thread.dispose(); 83 } 84 85 90 protected WorkerThread createWorker() 91 { 92 final String name = m_name + " Worker #" + m_level++; 93 94 final WorkerThread worker = newWorkerThread( name ); 95 worker.setDaemon( true ); 96 worker.start(); 97 return worker; 98 } 99 100 106 protected WorkerThread newWorkerThread( final String name ) 107 { 108 return new WorkerThread( this, m_threadGroup, name ); 109 } 110 111 118 public ThreadControl execute( final Runnable work ) 119 { 120 return execute( new ExecutableRunnable( work ) ); 121 } 122 123 129 public ThreadControl execute( final Executable work ) 130 { 131 final WorkerThread worker = getWorker(); 132 return worker.execute( work ); 133 } 134 135 141 protected String getName() 142 { 143 return m_name; 144 } 145 146 151 protected ThreadGroup getThreadGroup() 152 { 153 return m_threadGroup; 154 } 155 156 161 protected abstract WorkerThread getWorker(); 162 163 168 protected abstract void releaseWorker( final WorkerThread worker ); 169 } 170 | Popular Tags |