Tomcat's StandardService, Executor initialization and startup

Tomcat's Server initialization and startup process: http://donald-draper.iteye.com/blog/2327060
public class StandardService extends LifecycleMBeanBase implements Service {
    private static final String info =
        "org.apache.catalina.core.StandardService/1.0";
    private String name = null;
    private static final StringManager sm =
        StringManager.getManager(Constants.Package);
    private Server server = null;
    protected Connector connectors[] = new Connector[0];
    private final Object connectorsLock = new Object();
    protected ArrayList<Executor> executors = new ArrayList<Executor>();
    protected Container container = null;
    private ClassLoader parentClassLoader = null;

    // Initialize any Executors
    protected void initInternal() throws LifecycleException {
        for (Executor executor : findExecutors()) {
            if (executor instanceof LifecycleMBeanBase) {
                ((LifecycleMBeanBase) executor).setDomain(getDomain());
            }
            executor.init();
        }
    }
    protected void startInternal() throws LifecycleException {
        synchronized (executors) {
            for (Executor executor: executors) {
	        //start executor
                executor.start();
            }
        }
    }
}

Take a look at StandardThreadExecutor
public class StandardThreadExecutor extends LifecycleMBeanBase
        implements Executor, ResizableExecutor {
	  // ---------------------------------------------- Properties
    /**
     * Default thread priority
     */
    protected int threadPriority = Thread.NORM_PRIORITY;

    /**
     * Run threads in daemon or non-daemon state
     */
    protected boolean daemon = true;
    
    /**
     * Default name prefix for the thread name
     */
    protected String namePrefix = "tomcat-exec-";
    
    /**
     * max number of threads
     */
    protected int maxThreads = 200;
    
    /**
     * min number of threads
     */
    protected int minSpareThreads = 25;
      /**
     * idle time in milliseconds
     */
    protected int maxIdleTime = 60000;
    
    /**
     * The executor we use for this component
     */
    protected ThreadPoolExecutor executor = null;
    
    /**
     * the name of this thread pool
     */
    protected String name;
     /**
     * The maximum number of elements that can queue up before we reject them
     */
    protected int maxQueueSize = Integer.MAX_VALUE;
    private TaskQueue taskqueue = null;
    //Initialize, call initInternal of parent class, register to MBean to JVM
      protected void initInternal() throws LifecycleException {
        super.initInternal();
    }
    /**
     * Start the component and implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    //start up
    protected void startInternal() throws LifecycleException {
        //create task queue
        taskqueue = new TaskQueue(maxQueueSize);
	//create task thread factory
        TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority());
	//create thread executor
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf);
        executor.setThreadRenewalDelay(threadRenewalDelay);
        if (prestartminSpareThreads) {
            executor.prestartAllCoreThreads();
        }
        taskqueue.setParent(executor);
        setState(LifecycleState.STARTING);
    }

//View TaskQueue
/**
 * As task queue specifically designed to run with a thread pool executor.
 * The task queue is optimised to properly utilize threads within
 * a thread pool executor. If you use a normal queue, the executor will spawn threads
 * when there are idle threads and you wont be able to force items unto the queue itself
 *
 */
TaskQueue inherits LinkedBlockingQueue
public class TaskQueue extends LinkedBlockingQueue<Runnable> {
    private ThreadPoolExecutor parent = null;
    // no need to be volatile, the one times when we change and read it occur in
    // a single thread (the one that did stop a context and fired listeners)
    private Integer forcedRemainingCapacity = null;
    @Override
    //If possible, add Runnable to BlockingQueue, that is, if BlockingQueue can hold, return true, otherwise return false.
    public boolean offer(Runnable o) {
      //we can't do any checks
        if (parent==null) return super.offer(o);
        //we are maxed out on threads, simply queue the object
        if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
        //we have idle threads, just add it to the queue
        if (parent.getSubmittedCount()<(parent.getPoolSize())) return super.offer(o);
        //if we have less threads than maximum force creation of a new thread
        if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;
        //if we reached here, we need to add it to the queue
        return super.offer(o);
    }
    @Override
    //Get and remove the head Runnable of this queue. If you can't get it out immediately, you can wait for the time specified by the time parameter, and return null if you can't get it.
    public Runnable poll(long timeout, TimeUnit unit)
            throws InterruptedException {
        Runnable runnable = super.poll(timeout, unit);
        if (runnable == null && parent != null) {
            // the poll timed out, it gives an opportunity to stop the current
            // thread if needed to avoid memory leaks.
            parent.stopCurrentThreadIfNeeded();
        }
        return runnable;
    }

    @Override
    //Get the first object Runnable in the BlockingQueue. If the BlockingQueue is empty, block and enter the waiting state until a new object is added to the BlockingQueue
    public Runnable take() throws InterruptedException {
        if (parent != null && parent.currentThreadShouldBeStopped()) {
            return poll(parent.getKeepAliveTime(TimeUnit.MILLISECONDS),
                    TimeUnit.MILLISECONDS);
            // yes, this may return null (in case of timeout) which normally
            // does not occur with take()
            // but the ThreadPoolExecutor implementation allows this
        }
        return super.take();
    }
 }

//View TaskThreadFactory
/**
 * Simple task thread factory to use to create threads for an executor implementation.
 */
public class TaskThreadFactory implements ThreadFactory {
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;
    private final boolean daemon;
    private final int threadPriority;
    public TaskThreadFactory(String namePrefix, boolean daemon, int priority) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = namePrefix;
        this.daemon = daemon;
        this.threadPriority = priority;
    }

    @Override
    public Thread newThread(Runnable r) {
        TaskThread t = new TaskThread(group, r, namePrefix + threadNumber.getAndIncrement());
        t.setDaemon(daemon);
        t.setPriority(threadPriority);
        return t;
    }
}

View ThreadPoolExecutor
ThreadPoolExecutor inherits java.util.concurrent.ThreadPoolExecutor
public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor {}

Summary: Service starts, what it does is to create a thread executor

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993178&siteId=291194637