2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.thread;
21 import java.util.concurrent.ArrayBlockingQueue;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.RejectedExecutionException;
26 import java.util.concurrent.SynchronousQueue;
27 import java.util.concurrent.ThreadPoolExecutor;
28 import java.util.concurrent.TimeUnit;
30 import org.eclipse.jetty.util.component.AbstractLifeCycle;
31 import org.eclipse.jetty.util.component.LifeCycle;
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
35 /* ------------------------------------------------------------ */
37 * Jetty ThreadPool using java 5 ThreadPoolExecutor
38 * This class wraps a {@link ExecutorService} as a {@link ThreadPool} and
39 * {@link LifeCycle} interfaces so that it may be used by the Jetty <code>org.eclipse.jetty.server.Server</code>
41 public class ExecutorThreadPool extends AbstractLifeCycle implements ThreadPool, LifeCycle
43 private static final Logger LOG = Log.getLogger(ExecutorThreadPool.class);
44 private final ExecutorService _executor;
46 /* ------------------------------------------------------------ */
47 public ExecutorThreadPool(ExecutorService executor)
52 /* ------------------------------------------------------------ */
54 * Wraps an {@link ThreadPoolExecutor}.
55 * Max pool size is 256, pool thread timeout after 60 seconds and
56 * an unbounded {@link LinkedBlockingQueue} is used for the job queue;
58 public ExecutorThreadPool()
60 // Using an unbounded queue makes the maxThreads parameter useless
61 // Refer to ThreadPoolExecutor javadocs for details
62 this(new ThreadPoolExecutor(256, 256, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()));
65 /* ------------------------------------------------------------ */
67 * Wraps an {@link ThreadPoolExecutor}.
68 * Max pool size is 256, pool thread timeout after 60 seconds, and core pool size is 32 when queueSize >= 0.
69 * @param queueSize can be -1 for using an unbounded {@link LinkedBlockingQueue}, 0 for using a
70 * {@link SynchronousQueue}, greater than 0 for using a {@link ArrayBlockingQueue} of the given size.
72 public ExecutorThreadPool(int queueSize)
74 this(queueSize < 0 ? new ThreadPoolExecutor(256, 256, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()) :
75 queueSize == 0 ? new ThreadPoolExecutor(32, 256, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()) :
76 new ThreadPoolExecutor(32, 256, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize)));
79 /* ------------------------------------------------------------ */
81 * Wraps an {@link ThreadPoolExecutor} using
82 * an unbounded {@link LinkedBlockingQueue} is used for the jobs queue;
83 * @param corePoolSize must be equal to maximumPoolSize
84 * @param maximumPoolSize the maximum number of threads to allow in the pool
85 * @param keepAliveTime the max time a thread can remain idle, in milliseconds
87 public ExecutorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime)
89 this(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS);
92 /* ------------------------------------------------------------ */
94 * Wraps an {@link ThreadPoolExecutor} using
95 * an unbounded {@link LinkedBlockingQueue} is used for the jobs queue.
96 * @param corePoolSize must be equal to maximumPoolSize
97 * @param maximumPoolSize the maximum number of threads to allow in the pool
98 * @param keepAliveTime the max time a thread can remain idle
99 * @param unit the unit for the keepAliveTime
101 public ExecutorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)
103 this(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>());
106 /* ------------------------------------------------------------ */
109 * Wraps an {@link ThreadPoolExecutor}
110 * @param corePoolSize the number of threads to keep in the pool, even if they are idle
111 * @param maximumPoolSize the maximum number of threads to allow in the pool
112 * @param keepAliveTime the max time a thread can remain idle
113 * @param unit the unit for the keepAliveTime
114 * @param workQueue the queue to use for holding tasks before they are executed
116 public ExecutorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
118 this(new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue));
122 /* ------------------------------------------------------------ */
124 public void execute(Runnable job)
126 _executor.execute(job);
129 /* ------------------------------------------------------------ */
130 public boolean dispatch(Runnable job)
134 _executor.execute(job);
137 catch(RejectedExecutionException e)
144 /* ------------------------------------------------------------ */
145 public int getIdleThreads()
147 if (_executor instanceof ThreadPoolExecutor)
149 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
150 return tpe.getPoolSize() - tpe.getActiveCount();
155 /* ------------------------------------------------------------ */
156 public int getThreads()
158 if (_executor instanceof ThreadPoolExecutor)
160 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
161 return tpe.getPoolSize();
166 /* ------------------------------------------------------------ */
167 public boolean isLowOnThreads()
169 if (_executor instanceof ThreadPoolExecutor)
171 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
172 // getActiveCount() locks the thread pool, so execute it last
173 return tpe.getPoolSize() == tpe.getMaximumPoolSize() &&
174 tpe.getQueue().size() >= tpe.getPoolSize() - tpe.getActiveCount();
179 /* ------------------------------------------------------------ */
180 public void join() throws InterruptedException
182 _executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
185 /* ------------------------------------------------------------ */
187 protected void doStop() throws Exception
190 _executor.shutdownNow();