1 17 package org.apache.avalon.excalibur.thread.impl; 18 19 import org.apache.avalon.excalibur.pool.ObjectFactory; 20 import org.apache.avalon.excalibur.pool.Pool; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.activity.Executable; 24 import org.apache.avalon.framework.container.ContainerUtil; 25 import org.apache.avalon.framework.logger.LogEnabled; 26 import org.apache.avalon.framework.logger.Logger; 27 28 import org.apache.excalibur.thread.ThreadPool; 29 import org.apache.excalibur.thread.ThreadControl; 30 import org.apache.excalibur.thread.impl.AbstractThreadPool; 31 import org.apache.excalibur.thread.impl.WorkerThread; 32 33 38 class BasicThreadPool 39 extends AbstractThreadPool 40 implements ObjectFactory, LogEnabled, Disposable, ThreadPool 41 { 42 45 private Pool m_pool; 46 47 50 private Logger m_logger; 51 52 60 public BasicThreadPool( final ThreadGroup threadGroup, 61 final String name, 62 final Pool pool ) 63 throws Exception 64 { 65 super( name, threadGroup ); 66 if( null == pool ) 67 { 68 throw new NullPointerException ( "pool" ); 69 } 70 71 m_pool = pool; 72 } 73 74 79 public void enableLogging( final Logger logger ) 80 { 81 m_logger = logger; 82 ContainerUtil.enableLogging( m_pool, logger ); 83 } 84 85 88 public void dispose() 89 { 90 ContainerUtil.dispose( m_pool ); 91 m_pool = null; 92 } 93 94 99 public Object newInstance() 100 { 101 return createWorker(); 102 } 103 104 111 protected WorkerThread newWorkerThread( final String name ) 112 { 113 final SimpleWorkerThread thread = 114 new SimpleWorkerThread( this, getThreadGroup(), name ); 115 ContainerUtil.enableLogging( thread, m_logger.getChildLogger( "worker" ) ); 116 return thread; 117 } 118 119 public void decommission( final Object object ) 120 { 121 if( object instanceof WorkerThread ) 122 { 123 destroyWorker( (WorkerThread)object ); 124 } 125 } 126 127 132 public Class getCreatedClass() 133 { 134 return SimpleWorkerThread.class; 135 } 136 137 144 public ThreadControl execute( final Executable work ) 145 { 146 return execute( new ExecutableExecuteable( work ) ); 147 } 148 149 154 protected WorkerThread getWorker() 155 { 156 try 157 { 158 return (WorkerThread)m_pool.get(); 159 } 160 catch( final Exception e ) 161 { 162 final String message = 163 "Unable to access thread pool due to " + e; 164 throw new IllegalStateException ( message ); 165 } 166 } 167 168 176 protected void releaseWorker( final WorkerThread worker ) 177 { 178 worker.clearInterruptFlag(); 179 180 Pool pool = m_pool; if ( pool != null ) 188 { 189 pool.put( (SimpleWorkerThread)worker ); 190 } 191 } 192 } 193 | Popular Tags |