1 // ========================================================================
2 // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 // ========================================================================
14 
15 package org.mortbay.thread.concurrent;
16 
17 import java.util.concurrent.ArrayBlockingQueue;
18 import java.util.concurrent.BlockingQueue;
19 import java.util.concurrent.LinkedBlockingQueue;
20 import java.util.concurrent.RejectedExecutionException;
21 import java.util.concurrent.RejectedExecutionHandler;
22 import java.util.concurrent.SynchronousQueue;
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26 
27 import org.mortbay.component.LifeCycle;
28 import org.mortbay.log.Log;
29 
30 /* ------------------------------------------------------------ */
31 /** Jetty ThreadPool using java 5 ThreadPoolExecutor
32  * This class wraps a {@link ThreadPoolExecutor} with the {@link org.mortbay.thread.ThreadPool} and
33  * {@link LifeCycle} interfaces so that it may be used by the Jetty {@link org.mortbay.jetty.Server}
34  *
35  * @author gregw
36  *
37  */
38 public class ThreadPool extends ThreadPoolExecutor implements org.mortbay.thread.ThreadPool, LifeCycle
39 {
40 
41     /* ------------------------------------------------------------ */
42     /** Default constructor.
43      * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds and
44      * an unbounded {@link LinkedBlockingQueue} is used for the job queue;
45      */
ThreadPool()46     public ThreadPool()
47     {
48         super(32,256,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
49     }
50 
51     /* ------------------------------------------------------------ */
52     /** Default constructor.
53      * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds
54      * @param queueSize if -1, an unbounded {@link LinkedBlockingQueue} is used, if 0 then a
55      * {@link SynchronousQueue} is used, other a {@link ArrayBlockingQueue} of the given size is used.
56      */
ThreadPool(int queueSize)57     public ThreadPool(int queueSize)
58     {
59         super(32,256,60,TimeUnit.SECONDS,
60                 queueSize<0?new LinkedBlockingQueue<Runnable>()
61                         : (queueSize==0?new SynchronousQueue<Runnable>()
62                                 :new ArrayBlockingQueue<Runnable>(queueSize)));
63     }
64 
65     /* ------------------------------------------------------------ */
66     /** Size constructor.
67      * an unbounded {@link LinkedBlockingQueue} is used for the jobs queue;
68      */
ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)69     public ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)
70     {
71         super(corePoolSize,maximumPoolSize,keepAliveTime,unit,new LinkedBlockingQueue<Runnable>());
72     }
73 
74     /* ------------------------------------------------------------ */
ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)75     public ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
76     {
77         super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue);
78     }
79 
80     /* ------------------------------------------------------------ */
ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)81     public ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
82     {
83         super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,handler);
84     }
85 
86     /* ------------------------------------------------------------ */
ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)87     public ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
88     {
89         super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);
90     }
91 
92     /* ------------------------------------------------------------ */
ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)93     public ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
94     {
95         super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory);
96     }
97 
98     /* ------------------------------------------------------------ */
dispatch(Runnable job)99     public boolean dispatch(Runnable job)
100     {
101         try
102         {
103             execute(job);
104             return true;
105         }
106         catch(RejectedExecutionException e)
107         {
108             Log.warn(e);
109             return false;
110         }
111     }
112 
113     /* ------------------------------------------------------------ */
getIdleThreads()114     public int getIdleThreads()
115     {
116         return getPoolSize()-getActiveCount();
117     }
118 
119     /* ------------------------------------------------------------ */
getThreads()120     public int getThreads()
121     {
122         return getPoolSize();
123     }
124 
125     /* ------------------------------------------------------------ */
isLowOnThreads()126     public boolean isLowOnThreads()
127     {
128         return getActiveCount()>=getMaximumPoolSize();
129     }
130 
131     /* ------------------------------------------------------------ */
join()132     public void join() throws InterruptedException
133     {
134         this.awaitTermination(Long.MAX_VALUE,TimeUnit.MILLISECONDS);
135     }
136 
137     /* ------------------------------------------------------------ */
isFailed()138     public boolean isFailed()
139     {
140         return false;
141     }
142 
143     /* ------------------------------------------------------------ */
isRunning()144     public boolean isRunning()
145     {
146         return !isTerminated() && !isTerminating();
147     }
148 
149     /* ------------------------------------------------------------ */
isStarted()150     public boolean isStarted()
151     {
152         return !isTerminated() && !isTerminating();
153     }
154 
155     /* ------------------------------------------------------------ */
isStarting()156     public boolean isStarting()
157     {
158         return false;
159     }
160 
161     /* ------------------------------------------------------------ */
isStopped()162     public boolean isStopped()
163     {
164         return isTerminated();
165     }
166 
167     /* ------------------------------------------------------------ */
isStopping()168     public boolean isStopping()
169     {
170         return isTerminating();
171     }
172 
173     /* ------------------------------------------------------------ */
start()174     public void start() throws Exception
175     {
176         if (isTerminated() || isTerminating() || isShutdown())
177             throw new IllegalStateException("Cannot restart");
178     }
179 
180     /* ------------------------------------------------------------ */
stop()181     public void stop() throws Exception
182     {
183         super.shutdown();
184         if (!super.awaitTermination(60,TimeUnit.SECONDS))
185             super.shutdownNow();
186     }
187 
188     /* ------------------------------------------------------------ */
addLifeCycleListener(LifeCycle.Listener listener)189     public void addLifeCycleListener(LifeCycle.Listener listener)
190     {
191         throw new UnsupportedOperationException();
192     }
193 
194     /* ------------------------------------------------------------ */
removeLifeCycleListener(LifeCycle.Listener listener)195     public void removeLifeCycleListener(LifeCycle.Listener listener)
196     {
197     }
198 
199 }
200