001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.thread.concurrent;
016:
017: import java.util.concurrent.ArrayBlockingQueue;
018: import java.util.concurrent.BlockingQueue;
019: import java.util.concurrent.LinkedBlockingQueue;
020: import java.util.concurrent.RejectedExecutionException;
021: import java.util.concurrent.RejectedExecutionHandler;
022: import java.util.concurrent.SynchronousQueue;
023: import java.util.concurrent.ThreadFactory;
024: import java.util.concurrent.ThreadPoolExecutor;
025: import java.util.concurrent.TimeUnit;
026:
027: import org.mortbay.component.LifeCycle;
028: import org.mortbay.log.Log;
029:
030: /* ------------------------------------------------------------ */
031: /** Jetty ThreadPool using java 5 ThreadPoolExecutor
032: * This class wraps a {@link ThreadPoolExecutor} with the {@link org.mortbay.thread.ThreadPool} and
033: * {@link LifeCycle} interfaces so that it may be used by the Jetty {@link org.mortbay.jetty.Server}
034: *
035: * @author gregw
036: *
037: */
038: public class ThreadPool extends ThreadPoolExecutor implements
039: org.mortbay.thread.ThreadPool, LifeCycle {
040:
041: /* ------------------------------------------------------------ */
042: /** Default constructor.
043: * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds and
044: * an unbounded {@link LinkedBlockingQueue} is used for the job queue;
045: */
046: public ThreadPool() {
047: super (32, 256, 60, TimeUnit.SECONDS,
048: new LinkedBlockingQueue<Runnable>());
049: }
050:
051: /* ------------------------------------------------------------ */
052: /** Default constructor.
053: * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds
054: * @param queueSize if -1, an unbounded {@link LinkedBlockingQueue} is used, if 0 then a
055: * {@link SynchronousQueue} is used, other a {@link ArrayBlockingQueue} of the given size is used.
056: */
057: public ThreadPool(int queueSize) {
058: super (
059: 32,
060: 256,
061: 60,
062: TimeUnit.SECONDS,
063: queueSize < 0 ? new LinkedBlockingQueue<Runnable>()
064: : (queueSize == 0 ? new SynchronousQueue<Runnable>()
065: : new ArrayBlockingQueue<Runnable>(
066: queueSize)));
067: }
068:
069: /* ------------------------------------------------------------ */
070: /** Size constructor.
071: * an unbounded {@link LinkedBlockingQueue} is used for the jobs queue;
072: */
073: public ThreadPool(int corePoolSize, int maximumPoolSize,
074: long keepAliveTime, TimeUnit unit) {
075: super (corePoolSize, maximumPoolSize, keepAliveTime, unit,
076: new LinkedBlockingQueue<Runnable>());
077: }
078:
079: /* ------------------------------------------------------------ */
080: public ThreadPool(int corePoolSize, int maximumPoolSize,
081: long keepAliveTime, TimeUnit unit,
082: BlockingQueue<Runnable> workQueue) {
083: super (corePoolSize, maximumPoolSize, keepAliveTime, unit,
084: workQueue);
085: }
086:
087: /* ------------------------------------------------------------ */
088: public ThreadPool(int corePoolSize, int maximumPoolSize,
089: long keepAliveTime, TimeUnit unit,
090: BlockingQueue<Runnable> workQueue,
091: RejectedExecutionHandler handler) {
092: super (corePoolSize, maximumPoolSize, keepAliveTime, unit,
093: workQueue, handler);
094: }
095:
096: /* ------------------------------------------------------------ */
097: public ThreadPool(int corePoolSize, int maximumPoolSize,
098: long keepAliveTime, TimeUnit unit,
099: BlockingQueue<Runnable> workQueue,
100: ThreadFactory threadFactory,
101: RejectedExecutionHandler handler) {
102: super (corePoolSize, maximumPoolSize, keepAliveTime, unit,
103: workQueue, threadFactory, handler);
104: }
105:
106: /* ------------------------------------------------------------ */
107: public ThreadPool(int corePoolSize, int maximumPoolSize,
108: long keepAliveTime, TimeUnit unit,
109: BlockingQueue<Runnable> workQueue,
110: ThreadFactory threadFactory) {
111: super (corePoolSize, maximumPoolSize, keepAliveTime, unit,
112: workQueue, threadFactory);
113: }
114:
115: /* ------------------------------------------------------------ */
116: public boolean dispatch(Runnable job) {
117: try {
118: execute(job);
119: return true;
120: } catch (RejectedExecutionException e) {
121: Log.warn(e);
122: return false;
123: }
124: }
125:
126: /* ------------------------------------------------------------ */
127: public int getIdleThreads() {
128: return getPoolSize() - getActiveCount();
129: }
130:
131: /* ------------------------------------------------------------ */
132: public int getThreads() {
133: return getPoolSize();
134: }
135:
136: /* ------------------------------------------------------------ */
137: public boolean isLowOnThreads() {
138: return getActiveCount() >= getMaximumPoolSize();
139: }
140:
141: /* ------------------------------------------------------------ */
142: public void join() throws InterruptedException {
143: this .awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
144: }
145:
146: /* ------------------------------------------------------------ */
147: public boolean isFailed() {
148: return false;
149: }
150:
151: /* ------------------------------------------------------------ */
152: public boolean isRunning() {
153: return !isTerminated() && !isTerminating();
154: }
155:
156: /* ------------------------------------------------------------ */
157: public boolean isStarted() {
158: return !isTerminated() && !isTerminating();
159: }
160:
161: /* ------------------------------------------------------------ */
162: public boolean isStarting() {
163: return false;
164: }
165:
166: /* ------------------------------------------------------------ */
167: public boolean isStopped() {
168: return isTerminated();
169: }
170:
171: /* ------------------------------------------------------------ */
172: public boolean isStopping() {
173: return isTerminating();
174: }
175:
176: /* ------------------------------------------------------------ */
177: public void start() throws Exception {
178: if (isTerminated() || isTerminating() || isShutdown())
179: throw new IllegalStateException("Cannot restart");
180: }
181:
182: /* ------------------------------------------------------------ */
183: public void stop() throws Exception {
184: super .shutdown();
185: if (!super .awaitTermination(60, TimeUnit.SECONDS))
186: super.shutdownNow();
187: }
188:
189: }
|