1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.http.spi;
20
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.RejectedExecutionException;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26
27 import org.eclipse.jetty.util.component.ContainerLifeCycle;
28 import org.eclipse.jetty.util.component.LifeCycle;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
31 import org.eclipse.jetty.util.thread.ThreadPool;
32
33 public class DelegatingThreadPool extends ContainerLifeCycle implements ThreadPool
34 {
35 private static final Logger LOG = Log.getLogger(DelegatingThreadPool.class);
36
37 private Executor _executor;
38
39 public DelegatingThreadPool(Executor executor)
40 {
41 _executor=executor;
42 addBean(_executor);
43 }
44
45
46 public Executor getExecutor()
47 {
48 return _executor;
49 }
50
51
52 public void setExecutor(Executor executor)
53 {
54 if (isRunning())
55 throw new IllegalStateException(getState());
56 updateBean(_executor,executor);
57 _executor=executor;
58 }
59
60
61 @Override
62 public void execute(Runnable job)
63 {
64 _executor.execute(job);
65 }
66
67
68 @Override
69 public int getIdleThreads()
70 {
71 final Executor executor=_executor;
72 if (executor instanceof ThreadPool)
73 return ((ThreadPool)executor).getIdleThreads();
74
75 if (executor instanceof ThreadPoolExecutor)
76 {
77 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)executor;
78 return tpe.getPoolSize() - tpe.getActiveCount();
79 }
80 return -1;
81 }
82
83
84 @Override
85 public int getThreads()
86 {
87 final Executor executor=_executor;
88 if (executor instanceof ThreadPool)
89 return ((ThreadPool)executor).getThreads();
90
91 if (executor instanceof ThreadPoolExecutor)
92 {
93 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)executor;
94 return tpe.getPoolSize();
95 }
96 return -1;
97 }
98
99
100 @Override
101 public boolean isLowOnThreads()
102 {
103 final Executor executor=_executor;
104 if (executor instanceof ThreadPool)
105 return ((ThreadPool)executor).isLowOnThreads();
106
107 if (executor instanceof ThreadPoolExecutor)
108 {
109 final ThreadPoolExecutor tpe = (ThreadPoolExecutor)executor;
110
111 return tpe.getPoolSize() == tpe.getMaximumPoolSize() &&
112 tpe.getQueue().size() >= tpe.getPoolSize() - tpe.getActiveCount();
113 }
114 return false;
115 }
116
117
118 @Override
119 public void join() throws InterruptedException
120 {
121 final Executor executor=_executor;
122 if (executor instanceof ThreadPool)
123 ((ThreadPool)executor).join();
124 else if (executor instanceof ExecutorService)
125 ((ExecutorService)executor).awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
126 else
127 throw new IllegalStateException();
128 }
129
130
131 @Override
132 protected void doStop() throws Exception
133 {
134 super.doStop();
135 if (!(_executor instanceof LifeCycle) && (_executor instanceof ExecutorService))
136 ((ExecutorService)_executor).shutdownNow();
137 }
138
139 }