Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.thread;
 
 
 
 /* ------------------------------------------------------------ */
Jetty ThreadPool using java 5 ThreadPoolExecutor This class wraps a java.util.concurrent.ExecutorService as a ThreadPool and org.eclipse.jetty.util.component.LifeCycle interfaces so that it may be used by the Jetty org.eclipse.jetty.server.Server
 
 public class ExecutorThreadPool extends AbstractLifeCycle implements ThreadPoolLifeCycle
 {
     private static final Logger LOG = Log.getLogger(ExecutorThreadPool.class);
     private final ExecutorService _executor;
 
     /* ------------------------------------------------------------ */
     public ExecutorThreadPool(ExecutorService executor)
     {
          = executor;
     }
 
     /* ------------------------------------------------------------ */
    
Wraps an java.util.concurrent.ThreadPoolExecutor. Max pool size is 256, pool thread timeout after 60 seconds and an unbounded java.util.concurrent.LinkedBlockingQueue is used for the job queue;
 
     public ExecutorThreadPool()
     {
         // Using an unbounded queue makes the maxThreads parameter useless
         // Refer to ThreadPoolExecutor javadocs for details
         this(new ThreadPoolExecutor(256, 256, 60, .new LinkedBlockingQueue<Runnable>()));
     }
 
     /* ------------------------------------------------------------ */
    
Wraps an java.util.concurrent.ThreadPoolExecutor. Max pool size is 256, pool thread timeout after 60 seconds, and core pool size is 32 when queueSize >= 0.

Parameters:
queueSize can be -1 for using an unbounded java.util.concurrent.LinkedBlockingQueue, 0 for using a java.util.concurrent.SynchronousQueue, greater than 0 for using a java.util.concurrent.ArrayBlockingQueue of the given size.
 
     public ExecutorThreadPool(int queueSize)
     {
         this(queueSize < 0 ? new ThreadPoolExecutor(256, 256, 60, .new LinkedBlockingQueue<Runnable>()) :
                 queueSize == 0 ? new ThreadPoolExecutor(32, 256, 60, .new SynchronousQueue<Runnable>()) :
                         new ThreadPoolExecutor(32, 256, 60, .new ArrayBlockingQueue<Runnable>(queueSize)));
     }
 
     /* ------------------------------------------------------------ */
    
Wraps an java.util.concurrent.ThreadPoolExecutor using an unbounded java.util.concurrent.LinkedBlockingQueue is used for the jobs queue;

Parameters:
corePoolSize must be equal to maximumPoolSize
maximumPoolSize the maximum number of threads to allow in the pool
keepAliveTime the max time a thread can remain idle, in milliseconds
 
     public ExecutorThreadPool(int corePoolSizeint maximumPoolSizelong keepAliveTime)
     {
         this(corePoolSizemaximumPoolSizekeepAliveTime.);
     }
 
     /* ------------------------------------------------------------ */
    
Wraps an java.util.concurrent.ThreadPoolExecutor using an unbounded java.util.concurrent.LinkedBlockingQueue is used for the jobs queue.

Parameters:
corePoolSize must be equal to maximumPoolSize
maximumPoolSize the maximum number of threads to allow in the pool
keepAliveTime the max time a thread can remain idle
unit the unit for the keepAliveTime
    public ExecutorThreadPool(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unit)
    {
        this(corePoolSizemaximumPoolSizekeepAliveTimeunitnew LinkedBlockingQueue<Runnable>());
    }
    /* ------------------------------------------------------------ */

    
Wraps an java.util.concurrent.ThreadPoolExecutor

Parameters:
corePoolSize the number of threads to keep in the pool, even if they are idle
maximumPoolSize the maximum number of threads to allow in the pool
keepAliveTime the max time a thread can remain idle
unit the unit for the keepAliveTime
workQueue the queue to use for holding tasks before they are executed
    public ExecutorThreadPool(int corePoolSizeint maximumPoolSizelong keepAliveTimeTimeUnit unitBlockingQueue<RunnableworkQueue)
    {
        this(new ThreadPoolExecutor(corePoolSizemaximumPoolSizekeepAliveTimeunitworkQueue));
    }
    /* ------------------------------------------------------------ */
    public boolean dispatch(Runnable job)
    {
        try
        {
            .execute(job);
            return true;
        }
        catch(RejectedExecutionException e)
        {
            .warn(e);
            return false;
        }
    }
    /* ------------------------------------------------------------ */
    public int getIdleThreads()
    {
        if ( instanceof ThreadPoolExecutor)
        {
            final ThreadPoolExecutor tpe = (ThreadPoolExecutor);
            return tpe.getPoolSize() - tpe.getActiveCount();
        }
        return -1;
    }
    /* ------------------------------------------------------------ */
    public int getThreads()
    {
        if ( instanceof ThreadPoolExecutor)
        {
            final ThreadPoolExecutor tpe = (ThreadPoolExecutor);
            return tpe.getPoolSize();
        }
        return -1;
    }
    /* ------------------------------------------------------------ */
    public boolean isLowOnThreads()
    {
        if ( instanceof ThreadPoolExecutor)
        {
            final ThreadPoolExecutor tpe = (ThreadPoolExecutor);
            // getActiveCount() locks the thread pool, so execute it last
            return tpe.getPoolSize() == tpe.getMaximumPoolSize() &&
                    tpe.getQueue().size() >= tpe.getPoolSize() - tpe.getActiveCount();
        }
        return false;
    }
    /* ------------------------------------------------------------ */
    public void join() throws InterruptedException
    {
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        super.doStop();
        .shutdownNow();
    }
New to GrepCode? Check out our FAQ X