1
2
3
4
5
6
7
8
9
10
11
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
31
32
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
49
50
51
52
53 public ExecutorThreadPool()
54 {
55 this(new ThreadPoolExecutor(32,256,60,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>()));
56 }
57
58
59
60
61
62
63
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
75
76
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 }