1 16 package org.apache.cocoon.components.thread; 17 18 import org.apache.avalon.framework.logger.LogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 21 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 22 23 24 32 public class DefaultThreadPool 33 extends PooledExecutor 34 implements ThreadPool, LogEnabled 35 { 36 38 39 public static final String POLICY_DEFAULT = POLICY_RUN; 40 41 43 44 private ChannelWrapper m_channelWrapper; 45 46 47 private Logger m_logger; 48 49 50 private Queue m_queue; 51 52 53 private String m_blockPolicy; 54 55 56 private String m_name; 57 58 59 private boolean m_shutdownGraceful; 60 61 62 private int m_queueSize; 63 64 65 private int m_shutdownWaitTimeMs; 66 67 69 72 DefaultThreadPool( ) 73 { 74 this( new ChannelWrapper( ) ); 75 } 76 77 82 private DefaultThreadPool( final ChannelWrapper channel ) 83 { 84 super( channel ); 85 m_channelWrapper = channel; 86 } 87 88 90 95 public String getBlockPolicy( ) 96 { 97 return m_blockPolicy; 98 } 99 100 107 public int getMaxQueueSize( ) 108 { 109 return ( ( m_queueSize < 0 ) ? Integer.MAX_VALUE : m_queueSize ); 110 } 111 112 119 public int getMaximumQueueSize( ) 120 { 121 return m_queueSize; 122 } 123 124 127 public String getName( ) 128 { 129 return m_name; 130 } 131 132 138 public int getPriority( ) 139 { 140 return ((ThreadFactory)super.getThreadFactory()).getPriority(); 141 } 142 143 150 public int getQueueSize( ) 151 { 152 return m_queue.getQueueSize( ); 153 } 154 155 162 public boolean isQueued( ) 163 { 164 return m_queueSize != 0; 165 } 166 167 174 public void enableLogging( Logger logger ) 175 { 176 m_logger = logger; 177 } 178 179 186 public void execute( Runnable command ) 187 throws InterruptedException 188 { 189 if( getLogger( ).isDebugEnabled( ) ) 190 { 191 getLogger( ).debug( "Executing Command: " + command.toString( ) + 192 ",pool=" + getName( ) ); 193 } 194 195 super.execute( command ); 196 } 197 198 201 public void shutdown( ) 202 { 203 if( m_shutdownGraceful ) 204 { 205 shutdownAfterProcessingCurrentlyQueuedTasks( ); 206 } 207 else 208 { 209 shutdownNow( ); 210 } 211 212 try 213 { 214 if( getShutdownWaitTimeMs( ) > 0 ) 215 { 216 if( ! awaitTerminationAfterShutdown( getShutdownWaitTimeMs( ) ) ) 217 { 218 getLogger( ).warn( "running commands have not terminated within " + 219 getShutdownWaitTimeMs( ) + 220 "ms. Will shut them down by interruption" ); 221 interruptAll( ); 222 shutdownNow( ); 223 } 224 } 225 226 awaitTerminationAfterShutdown( ); 227 } 228 catch( final InterruptedException ie ) 229 { 230 getLogger( ).error( "cannot shutdown ThreadPool", ie ); 231 } 232 } 233 234 239 void setBlockPolicy( final String blockPolicy ) 240 { 241 m_blockPolicy = blockPolicy; 242 243 if( POLICY_ABORT.equalsIgnoreCase( blockPolicy ) ) 244 { 245 abortWhenBlocked( ); 246 } 247 else if( POLICY_DISCARD.equalsIgnoreCase( blockPolicy ) ) 248 { 249 discardWhenBlocked( ); 250 } 251 else if( POLICY_DISCARD_OLDEST.equalsIgnoreCase( blockPolicy ) ) 252 { 253 discardOldestWhenBlocked( ); 254 } 255 else if( POLICY_RUN.equalsIgnoreCase( blockPolicy ) ) 256 { 257 runWhenBlocked( ); 258 } 259 else if( POLICY_WAIT.equalsIgnoreCase( blockPolicy ) ) 260 { 261 waitWhenBlocked( ); 262 } 263 else 264 { 265 final StringBuffer msg = new StringBuffer ( ); 266 msg.append( "WARNING: Unknown block-policy configuration \"" ) 267 .append( blockPolicy ); 268 msg.append( "\". Should be one of \"" ).append( POLICY_ABORT ); 269 msg.append( "\",\"" ).append( POLICY_DISCARD ); 270 msg.append( "\",\"" ).append( POLICY_DISCARD_OLDEST ); 271 msg.append( "\",\"" ).append( POLICY_RUN ); 272 msg.append( "\",\"" ).append( POLICY_WAIT ); 273 msg.append( "\". Will use \"" ).append( POLICY_DEFAULT ).append( "\"" ); 274 getLogger( ).warn( msg.toString( ) ); 275 setBlockPolicy( POLICY_DEFAULT ); 276 } 277 } 278 279 284 void setName( String name ) 285 { 286 m_name = name; 287 } 288 289 294 void setQueue( final int queueSize ) 295 { 296 if( queueSize != 0 ) 297 { 298 if( queueSize > 0 ) 299 { 300 m_queue = new BoundedQueue( queueSize ); 301 } 302 else 303 { 304 m_queue = new LinkedQueue( ); 305 } 306 } 307 else 308 { 309 m_queue = new SynchronousChannel( ); 310 } 311 312 m_queueSize = queueSize; 313 m_channelWrapper.setChannel( m_queue ); 314 } 315 316 321 void setShutdownGraceful( boolean shutdownGraceful ) 322 { 323 m_shutdownGraceful = shutdownGraceful; 324 } 325 326 331 boolean isShutdownGraceful( ) 332 { 333 return m_shutdownGraceful; 334 } 335 336 341 void setShutdownWaitTimeMs( int shutdownWaitTimeMs ) 342 { 343 m_shutdownWaitTimeMs = shutdownWaitTimeMs; 344 } 345 346 351 int getShutdownWaitTimeMs( ) 352 { 353 return m_shutdownWaitTimeMs; 354 } 355 356 361 private Logger getLogger( ) 362 { 363 return m_logger; 364 } 365 } 366 | Popular Tags |