1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.util.thread;
15
16 import java.util.concurrent.ArrayBlockingQueue;
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.LinkedBlockingQueue;
20 import java.util.concurrent.RejectedExecutionException;
21 import java.util.concurrent.SynchronousQueue;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jetty.util.component.AbstractLifeCycle;
26 import org.eclipse.jetty.util.component.LifeCycle;
27 import org.eclipse.jetty.util.log.Log;
28
29 /* ------------------------------------------------------------ */
30 /** Jetty ThreadPool using java 5 ThreadPoolExecutor
31 * This class wraps a {@link ExecutorService} as a {@link ThreadPool} and
32 * {@link LifeCycle} interfaces so that it may be used by the Jetty {@link org.eclipse.jetty.Server}
33 *
34 *
35 *
36 */
37 public class ExecutorThreadPool extends AbstractLifeCycle implements ThreadPool, LifeCycle
38 {
39 private final ExecutorService _executor;
40
41 /* ------------------------------------------------------------ */
42 public ExecutorThreadPool(ExecutorService executor)
43 {
44 _executor=executor;
45 }
46
47 /* ------------------------------------------------------------ */
48 /** constructor.
49 * Wraps an {@link ThreadPoolExecutor}.
50 * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds and
51 * an unbounded {@link LinkedBlockingQueue} is used for the job queue;
52 */
53 public ExecutorThreadPool()
54 {
55 this(new ThreadPoolExecutor(32,256,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>()));
56 }
57
58 /* ------------------------------------------------------------ */
59 /** constructor.
60 * Wraps an {@link ThreadPoolExecutor}.
61 * Core size is 32, max pool size is 256, pool thread timeout after 60 seconds
62 * @param queueSize if -1, an unbounded {@link LinkedBlockingQueue} is used, if 0 then a
63 * {@link SynchronousQueue} is used, other a {@link ArrayBlockingQueue} of the given size is used.
64 */
65 public ExecutorThreadPool(int queueSize)
66 {
67 this(new ThreadPoolExecutor(32,256,60,TimeUnit.SECONDS,
68 queueSize<0?new LinkedBlockingQueue<Runnable>()
69 : (queueSize==0?new SynchronousQueue<Runnable>()
70 :new ArrayBlockingQueue<Runnable>(queueSize))));
71 }
72
73 /* ------------------------------------------------------------ */
74 /** constructor.
75 * Wraps an {@link ThreadPoolExecutor} using
76 * an unbounded {@link LinkedBlockingQueue} is used for the jobs queue;
77 */
78 public ExecutorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)
79 {
80 this(new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,new LinkedBlockingQueue<Runnable>()));
81 }
82
83 /* ------------------------------------------------------------ */
84 public ExecutorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
85 {
86 this(new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue));
87 }
88
89
90 /* ------------------------------------------------------------ */
91 public boolean dispatch(Runnable job)
92 {
93 try
94 {
95 _executor.execute(job);
96 return true;
97 }
98 catch(RejectedExecutionException e)
99 {
100 Log.warn(e);
101 return false;
102 }
103 }
104
105 /* ------------------------------------------------------------ */
106 public int getIdleThreads()
107 {
108 if (_executor instanceof ThreadPoolExecutor)
109 {
110 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
111 return tpe.getPoolSize() -tpe.getActiveCount();
112 }
113 return -1;
114 }
115
116 /* ------------------------------------------------------------ */
117 public int getThreads()
118 {
119 if (_executor instanceof ThreadPoolExecutor)
120 {
121 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
122 return tpe.getPoolSize();
123 }
124 return -1;
125 }
126
127 /* ------------------------------------------------------------ */
128 public boolean isLowOnThreads()
129 {
130 if (_executor instanceof ThreadPoolExecutor)
131 {
132 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)_executor;
133 return tpe.getActiveCount()>=tpe.getMaximumPoolSize();
134 }
135 return false;
136 }
137
138 /* ------------------------------------------------------------ */
139 public void join() throws InterruptedException
140 {
141 _executor.awaitTermination(Long.MAX_VALUE,TimeUnit.MILLISECONDS);
142 }
143
144 /* ------------------------------------------------------------ */
145 @Override
146 protected void doStart() throws Exception
147 {
148 super.doStart();
149 }
150
151 /* ------------------------------------------------------------ */
152 @Override
153 protected void doStop() throws Exception
154 {
155 super.doStop();
156 _executor.shutdownNow();
157 }
158
159 }