1 8 package org.apache.avalon.excalibur.thread.impl; 9 10 import org.apache.avalon.excalibur.lang.ThreadContext; 11 import org.apache.avalon.excalibur.pool.Poolable; 12 import org.apache.avalon.excalibur.pool.Pool; 13 import org.apache.avalon.excalibur.thread.ThreadControl; 14 import org.apache.avalon.framework.activity.Executable; 15 import org.apache.avalon.framework.logger.LogEnabled; 16 import org.apache.avalon.framework.logger.LogKitLogger; 17 import org.apache.avalon.framework.logger.Logger; 18 19 25 class WorkerThread 26 extends Thread 27 implements Poolable, LogEnabled 28 { 29 private Logger m_logger; 30 private Pool m_pool; 31 32 private Executable m_work; 33 private DefaultThreadControl m_threadControl; 34 private ThreadContext m_context; 35 private boolean m_alive; 36 37 40 protected WorkerThread( final ThreadGroup group, 41 final String name, 42 final Pool pool, 43 final ThreadContext context ) 44 { 45 super( group, name ); 46 47 m_pool = pool; 48 m_context = context; 49 50 m_work = null; 51 52 m_alive = true; 53 54 setDaemon( false ); 55 } 56 57 public void enableLogging( final Logger logger ) 58 { 59 m_logger = logger; 60 } 61 62 65 public final synchronized void run() 66 { 67 debug( "starting." ); 68 69 72 while( m_alive ) 73 { 74 waitUntilCondition( true ); 75 76 debug( "running." ); 77 78 try 79 { 80 if( null != m_context ) ThreadContext.setThreadContext( m_context ); 81 m_work.execute(); 82 m_threadControl.finish( null ); 83 } 84 catch( final ThreadDeath threadDeath ) 85 { 86 debug( "thread has died." ); 87 m_threadControl.finish( threadDeath ); 88 throw threadDeath; 91 } 92 catch( final Throwable throwable ) 93 { 94 debug( "error caught: " + throwable ); 96 m_threadControl.finish( throwable ); 97 } 98 finally 99 { 100 debug( "done." ); 101 m_work = null; 102 m_threadControl = null; 103 } 104 105 notify(); 109 110 if( null != m_pool ) 112 { 113 m_pool.put( this ); 114 } 115 else 116 { 117 m_alive = false; 118 } 119 } 120 } 121 122 128 public void dispose() 129 { 130 debug( "destroying." ); 131 m_alive = false; 132 waitUntilCondition( false ); 133 } 134 135 protected synchronized ThreadControl execute( final Executable work ) 136 { 137 m_work = work; 138 m_threadControl = new DefaultThreadControl( this ); 139 140 debug( "notifying this worker." ); 141 notify(); 142 143 return m_threadControl; 144 } 145 146 150 protected synchronized void executeAndWait( final Executable work ) 151 { 152 execute( work ); 153 waitUntilCondition( false ); 154 } 155 156 private synchronized void waitUntilCondition( final boolean hasWork ) 157 { 158 while( hasWork == (null == m_work) ) 159 { 160 try 161 { 162 debug( "waiting." ); 163 wait(); 164 debug( "notified." ); 165 } 166 catch( final InterruptedException ie ) {} 167 } 168 } 169 170 private void debug( final String message ) 171 { 172 if( false ) 173 { 174 final String output = getName() + ": " + message; 175 m_logger.debug( output ); 176 } 178 } 179 } 180 | Popular Tags |