1 17 package org.apache.excalibur.event.command; 18 19 import org.apache.avalon.framework.logger.NullLogger; 20 import org.apache.avalon.framework.parameters.ParameterException; 21 import org.apache.avalon.framework.parameters.Parameterizable; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.excalibur.util.SystemUtil; 24 25 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 26 27 35 public final class TPCThreadManager extends AbstractThreadManager implements Parameterizable 36 { 37 private PooledExecutor m_threadPool; 38 private int m_processors = -1; 39 private int m_threadsPerProcessor = 1; 40 private int m_keepAliveTime = 300000; 41 private boolean m_hardShutdown = false; 42 43 81 public void parameterize( Parameters parameters ) throws ParameterException 82 { 83 m_processors = Math.max(1, parameters.getParameterAsInteger( "processors", 1 ) ); 84 85 m_threadsPerProcessor = 86 Math.max( parameters.getParameterAsInteger( "threads-per-processor", 1 ), 1 ); 87 88 m_keepAliveTime = parameters.getParameterAsInteger("keep-alive-time", 300000); 89 90 setSleepTime( parameters.getParameterAsLong( "sleep-time", 1000L ) ); 91 92 m_hardShutdown = ( parameters.getParameterAsBoolean( "force-shutdown", false ) ); 93 } 94 95 public void initialize() throws Exception 96 { 97 if( m_processors < 1 ) 98 { 99 m_processors = Math.max( 1, SystemUtil.numProcessors() ); 100 } 101 102 if( isInitialized() ) 103 { 104 throw new IllegalStateException ( "ThreadManager is already initailized" ); 105 } 106 107 final int maxPoolSize = ( m_processors * m_threadsPerProcessor ) + 1; 108 m_threadPool = new PooledExecutor( m_processors + 1 ); 109 m_threadPool.setMinimumPoolSize( 2 ); m_threadPool.setMaximumPoolSize( maxPoolSize ); 111 m_threadPool.waitWhenBlocked(); 112 if( maxPoolSize == 2 ) 113 { 114 m_threadPool.setKeepAliveTime( -1 ); 125 } 126 else 127 { 128 m_threadPool.setKeepAliveTime( m_keepAliveTime ); 129 } 130 131 if( null == getLogger() ) 132 { 133 this.enableLogging( new NullLogger() ); 134 } 135 136 setExecutor( m_threadPool ); 137 138 super.initialize(); 139 } 140 141 protected final void doDispose() 142 { 143 if ( m_hardShutdown ) 144 { 145 m_threadPool.shutdownNow(); 146 } 147 else 148 { 149 m_threadPool.shutdownAfterProcessingCurrentlyQueuedTasks(); 150 } 151 152 m_threadPool.interruptAll(); 153 154 try 155 { 156 if ( !m_threadPool.awaitTerminationAfterShutdown( getSleepTime() ) ) 157 { 158 getLogger().warn("Thread pool took longer than " + getSleepTime() + 159 " ms to shut down"); 160 } 161 } 162 catch (InterruptedException ie) 163 { 164 getLogger().warn("Thread pool was interrupted while waiting for shutdown to complete.", ie); 165 } 166 } 167 } 168 | Popular Tags |