author | chegar |
Wed, 11 Nov 2015 11:31:21 +0000 | |
changeset 33680 | 56aa0b79bf5a |
parent 29937 | c774371adf92 |
child 37620 | 230612715768 |
permissions | -rw-r--r-- |
4 | 1 |
/* |
29937 | 2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
4 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5555 | 7 |
* published by the Free Software Foundation. Oracle designates this |
4 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5555 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5555 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
4 | 24 |
*/ |
25 |
||
26 |
package com.sun.corba.se.impl.orbutil.threadpool; |
|
27 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
28 |
import java.io.IOException; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
29 |
import java.io.Closeable; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
30 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
31 |
import java.security.AccessController; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
32 |
import java.security.PrivilegedAction; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
33 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
34 |
import java.util.List; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
35 |
import java.util.ArrayList; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
36 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
37 |
import java.util.concurrent.atomic.AtomicInteger; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
38 |
import java.util.concurrent.atomic.AtomicLong; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
39 |
|
4 | 40 |
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException; |
41 |
import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool; |
|
42 |
import com.sun.corba.se.spi.orbutil.threadpool.Work; |
|
43 |
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue; |
|
44 |
||
45 |
import com.sun.corba.se.impl.orbutil.ORBConstants; |
|
46 |
import com.sun.corba.se.impl.orbutil.threadpool.WorkQueueImpl; |
|
47 |
||
48 |
import com.sun.corba.se.spi.monitoring.MonitoringConstants; |
|
49 |
import com.sun.corba.se.spi.monitoring.MonitoredObject; |
|
50 |
import com.sun.corba.se.spi.monitoring.MonitoringFactories; |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
51 |
import com.sun.corba.se.spi.orb.ORB; |
4 | 52 |
import com.sun.corba.se.spi.monitoring.LongMonitoredAttributeBase; |
53 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
54 |
import com.sun.corba.se.impl.logging.ORBUtilSystemException; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
55 |
import com.sun.corba.se.impl.orbutil.ORBConstants; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
56 |
import com.sun.corba.se.spi.logging.CORBALogDomains; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
57 |
|
4 | 58 |
public class ThreadPoolImpl implements ThreadPool |
59 |
{ |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
60 |
// serial counter useful for debugging |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
61 |
private static AtomicInteger threadCounter = new AtomicInteger(0); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
62 |
private static final ORBUtilSystemException wrapper = |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
63 |
ORBUtilSystemException.get(CORBALogDomains.RPC_TRANSPORT); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
64 |
|
4 | 65 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
66 |
// Any time currentThreadCount and/or availableWorkerThreads is updated |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
67 |
// or accessed this ThreadPool's WorkQueue must be locked. And, it is |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
68 |
// expected that this ThreadPool's WorkQueue is the only object that |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
69 |
// updates and accesses these values directly and indirectly though a |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
70 |
// call to a method in this ThreadPool. If any call to update or access |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
71 |
// those values must synchronized on this ThreadPool's WorkQueue. |
4 | 72 |
private WorkQueue workQueue; |
73 |
||
74 |
// Stores the number of available worker threads |
|
75 |
private int availableWorkerThreads = 0; |
|
76 |
||
77 |
// Stores the number of threads in the threadpool currently |
|
78 |
private int currentThreadCount = 0; |
|
79 |
||
80 |
// Minimum number of worker threads created at instantiation of the threadpool |
|
81 |
private int minWorkerThreads = 0; |
|
82 |
||
83 |
// Maximum number of worker threads in the threadpool |
|
84 |
private int maxWorkerThreads = 0; |
|
85 |
||
86 |
// Inactivity timeout value for worker threads to exit and stop running |
|
87 |
private long inactivityTimeout; |
|
88 |
||
89 |
// Indicates if the threadpool is bounded or unbounded |
|
90 |
private boolean boundedThreadPool = false; |
|
91 |
||
92 |
// Running count of the work items processed |
|
93 |
// Set the value to 1 so that divide by zero is avoided in |
|
94 |
// averageWorkCompletionTime() |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
95 |
private AtomicLong processedCount = new AtomicLong(1); |
4 | 96 |
|
97 |
// Running aggregate of the time taken in millis to execute work items |
|
98 |
// processed by the threads in the threadpool |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
99 |
private AtomicLong totalTimeTaken = new AtomicLong(0); |
4 | 100 |
|
101 |
// Name of the ThreadPool |
|
102 |
private String name; |
|
103 |
||
104 |
// MonitoredObject for ThreadPool |
|
105 |
private MonitoredObject threadpoolMonitoredObject; |
|
106 |
||
107 |
// ThreadGroup in which threads should be created |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
108 |
private ThreadGroup threadGroup; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
109 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
110 |
Object workersLock = new Object(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
111 |
List<WorkerThread> workers = new ArrayList<>(); |
4 | 112 |
|
113 |
/** |
|
114 |
* This constructor is used to create an unbounded threadpool |
|
115 |
*/ |
|
116 |
public ThreadPoolImpl(ThreadGroup tg, String threadpoolName) { |
|
117 |
inactivityTimeout = ORBConstants.DEFAULT_INACTIVITY_TIMEOUT; |
|
118 |
maxWorkerThreads = Integer.MAX_VALUE; |
|
119 |
workQueue = new WorkQueueImpl(this); |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
120 |
threadGroup = tg; |
4 | 121 |
name = threadpoolName; |
122 |
initializeMonitoring(); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* This constructor is used to create an unbounded threadpool |
|
127 |
* in the ThreadGroup of the current thread |
|
128 |
*/ |
|
129 |
public ThreadPoolImpl(String threadpoolName) { |
|
130 |
this( Thread.currentThread().getThreadGroup(), threadpoolName ) ; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* This constructor is used to create bounded threadpool |
|
135 |
*/ |
|
136 |
public ThreadPoolImpl(int minSize, int maxSize, long timeout, |
|
137 |
String threadpoolName) |
|
138 |
{ |
|
139 |
minWorkerThreads = minSize; |
|
140 |
maxWorkerThreads = maxSize; |
|
141 |
inactivityTimeout = timeout; |
|
142 |
boundedThreadPool = true; |
|
143 |
workQueue = new WorkQueueImpl(this); |
|
144 |
name = threadpoolName; |
|
145 |
for (int i = 0; i < minWorkerThreads; i++) { |
|
146 |
createWorkerThread(); |
|
147 |
} |
|
148 |
initializeMonitoring(); |
|
149 |
} |
|
150 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
151 |
// Note that this method should not return until AFTER all threads have died. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
152 |
public void close() throws IOException { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
153 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
154 |
// Copy to avoid concurrent modification problems. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
155 |
List<WorkerThread> copy = null; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
156 |
synchronized (workersLock) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
157 |
copy = new ArrayList<>(workers); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
158 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
159 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
160 |
for (WorkerThread wt : copy) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
161 |
wt.close(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
162 |
while (wt.getState() != Thread.State.TERMINATED) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
163 |
try { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
164 |
wt.join(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
165 |
} catch (InterruptedException exc) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
166 |
wrapper.interruptedJoinCallWhileClosingThreadPool(exc, wt, this); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
167 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
168 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
169 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
170 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
171 |
threadGroup = null; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
172 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
173 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
174 |
|
4 | 175 |
// Setup monitoring for this threadpool |
176 |
private void initializeMonitoring() { |
|
177 |
// Get root monitored object |
|
178 |
MonitoredObject root = MonitoringFactories.getMonitoringManagerFactory(). |
|
179 |
createMonitoringManager(MonitoringConstants.DEFAULT_MONITORING_ROOT, null). |
|
180 |
getRootMonitoredObject(); |
|
181 |
||
182 |
// Create the threadpool monitoring root |
|
183 |
MonitoredObject threadPoolMonitoringObjectRoot = root.getChild( |
|
184 |
MonitoringConstants.THREADPOOL_MONITORING_ROOT); |
|
185 |
if (threadPoolMonitoringObjectRoot == null) { |
|
186 |
threadPoolMonitoringObjectRoot = MonitoringFactories. |
|
187 |
getMonitoredObjectFactory().createMonitoredObject( |
|
188 |
MonitoringConstants.THREADPOOL_MONITORING_ROOT, |
|
189 |
MonitoringConstants.THREADPOOL_MONITORING_ROOT_DESCRIPTION); |
|
190 |
root.addChild(threadPoolMonitoringObjectRoot); |
|
191 |
} |
|
192 |
threadpoolMonitoredObject = MonitoringFactories. |
|
193 |
getMonitoredObjectFactory(). |
|
194 |
createMonitoredObject(name, |
|
195 |
MonitoringConstants.THREADPOOL_MONITORING_DESCRIPTION); |
|
196 |
||
197 |
threadPoolMonitoringObjectRoot.addChild(threadpoolMonitoredObject); |
|
198 |
||
199 |
LongMonitoredAttributeBase b1 = new |
|
200 |
LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS, |
|
201 |
MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS_DESCRIPTION) { |
|
202 |
public Object getValue() { |
|
203 |
return new Long(ThreadPoolImpl.this.currentNumberOfThreads()); |
|
204 |
} |
|
205 |
}; |
|
206 |
threadpoolMonitoredObject.addAttribute(b1); |
|
207 |
LongMonitoredAttributeBase b2 = new |
|
208 |
LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_NUMBER_OF_AVAILABLE_THREADS, |
|
209 |
MonitoringConstants.THREADPOOL_CURRENT_NUMBER_OF_THREADS_DESCRIPTION) { |
|
210 |
public Object getValue() { |
|
211 |
return new Long(ThreadPoolImpl.this.numberOfAvailableThreads()); |
|
212 |
} |
|
213 |
}; |
|
214 |
threadpoolMonitoredObject.addAttribute(b2); |
|
215 |
LongMonitoredAttributeBase b3 = new |
|
216 |
LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_NUMBER_OF_BUSY_THREADS, |
|
217 |
MonitoringConstants.THREADPOOL_NUMBER_OF_BUSY_THREADS_DESCRIPTION) { |
|
218 |
public Object getValue() { |
|
219 |
return new Long(ThreadPoolImpl.this.numberOfBusyThreads()); |
|
220 |
} |
|
221 |
}; |
|
222 |
threadpoolMonitoredObject.addAttribute(b3); |
|
223 |
LongMonitoredAttributeBase b4 = new |
|
224 |
LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_AVERAGE_WORK_COMPLETION_TIME, |
|
225 |
MonitoringConstants.THREADPOOL_AVERAGE_WORK_COMPLETION_TIME_DESCRIPTION) { |
|
226 |
public Object getValue() { |
|
227 |
return new Long(ThreadPoolImpl.this.averageWorkCompletionTime()); |
|
228 |
} |
|
229 |
}; |
|
230 |
threadpoolMonitoredObject.addAttribute(b4); |
|
231 |
LongMonitoredAttributeBase b5 = new |
|
232 |
LongMonitoredAttributeBase(MonitoringConstants.THREADPOOL_CURRENT_PROCESSED_COUNT, |
|
233 |
MonitoringConstants.THREADPOOL_CURRENT_PROCESSED_COUNT_DESCRIPTION) { |
|
234 |
public Object getValue() { |
|
235 |
return new Long(ThreadPoolImpl.this.currentProcessedCount()); |
|
236 |
} |
|
237 |
}; |
|
238 |
threadpoolMonitoredObject.addAttribute(b5); |
|
239 |
||
240 |
// Add the monitored object for the WorkQueue |
|
241 |
||
242 |
threadpoolMonitoredObject.addChild( |
|
243 |
((WorkQueueImpl)workQueue).getMonitoredObject()); |
|
244 |
} |
|
245 |
||
246 |
// Package private method to get the monitored object for this |
|
247 |
// class |
|
248 |
MonitoredObject getMonitoredObject() { |
|
249 |
return threadpoolMonitoredObject; |
|
250 |
} |
|
251 |
||
252 |
public WorkQueue getAnyWorkQueue() |
|
253 |
{ |
|
254 |
return workQueue; |
|
255 |
} |
|
256 |
||
257 |
public WorkQueue getWorkQueue(int queueId) |
|
258 |
throws NoSuchWorkQueueException |
|
259 |
{ |
|
260 |
if (queueId != 0) |
|
261 |
throw new NoSuchWorkQueueException(); |
|
262 |
return workQueue; |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* To be called from the workqueue when work is added to the |
|
267 |
* workQueue. This method would create new threads if required |
|
268 |
* or notify waiting threads on the queue for available work |
|
269 |
*/ |
|
270 |
void notifyForAvailableWork(WorkQueue aWorkQueue) { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
271 |
synchronized (aWorkQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
272 |
if (availableWorkerThreads < aWorkQueue.workItemsInQueue()) { |
4 | 273 |
createWorkerThread(); |
274 |
} else { |
|
275 |
aWorkQueue.notify(); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
281 |
private Thread createWorkerThreadHelper( String name ) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
282 |
// Thread creation needs to be in a doPrivileged block |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
283 |
// if there is a non-null security manager for two reasons: |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
284 |
// 1. The creation of a thread in a specific ThreadGroup |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
285 |
// is a privileged operation. Lack of a doPrivileged |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
286 |
// block here causes an AccessControlException |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
287 |
// (see bug 6268145). |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
288 |
// 2. We want to make sure that the permissions associated |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
289 |
// with this thread do NOT include the permissions of |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
290 |
// the current thread that is calling this method. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
291 |
// This leads to problems in the app server where |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
292 |
// some threads in the ThreadPool randomly get |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
293 |
// bad permissions, leading to unpredictable |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
294 |
// permission errors (see bug 6021011). |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
295 |
// |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
296 |
// A Java thread contains a stack of call frames, |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
297 |
// one for each method called that has not yet returned. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
298 |
// Each method comes from a particular class. The class |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
299 |
// was loaded by a ClassLoader which has an associated |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
300 |
// CodeSource, and this determines the Permissions |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
301 |
// for all methods in that class. The current |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
302 |
// Permissions for the thread are the intersection of |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
303 |
// all Permissions for the methods on the stack. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
304 |
// This is part of the Security Context of the thread. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
305 |
// |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
306 |
// When a thread creates a new thread, the new thread |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
307 |
// inherits the security context of the old thread. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
308 |
// This is bad in a ThreadPool, because different |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
309 |
// creators of threads may have different security contexts. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
310 |
// This leads to occasional unpredictable errors when |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
311 |
// a thread is re-used in a different security context. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
312 |
// |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
313 |
// Avoiding this problem is simple: just do the thread |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
314 |
// creation in a doPrivileged block. This sets the |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
315 |
// inherited security context to that of the code source |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
316 |
// for the ORB code itself, which contains all permissions |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
317 |
// in either Java SE or Java EE. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
318 |
WorkerThread thread = new WorkerThread(threadGroup, name); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
319 |
synchronized (workersLock) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
320 |
workers.add(thread); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
321 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
322 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
323 |
// The thread must be set to a daemon thread so the |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
324 |
// VM can exit if the only threads left are PooledThreads |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
325 |
// or other daemons. We don't want to rely on the |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
326 |
// calling thread always being a daemon. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
327 |
// Note that no exception is possible here since we |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
328 |
// are inside the doPrivileged block. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
329 |
thread.setDaemon(true); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
330 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
331 |
wrapper.workerThreadCreated(thread, thread.getContextClassLoader()); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
332 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
333 |
thread.start(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
334 |
return null; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
335 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
336 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
337 |
|
4 | 338 |
/** |
339 |
* To be called from the workqueue to create worker threads when none |
|
340 |
* available. |
|
341 |
*/ |
|
342 |
void createWorkerThread() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
343 |
final String name = getName(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
344 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
345 |
try { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
346 |
if (System.getSecurityManager() == null) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
347 |
createWorkerThreadHelper(name); |
4 | 348 |
} else { |
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
349 |
// If we get here, we need to create a thread. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
350 |
AccessController.doPrivileged( |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
351 |
new PrivilegedAction() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
352 |
public Object run() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
353 |
return createWorkerThreadHelper(name); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
354 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
355 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
356 |
); |
4 | 357 |
} |
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
358 |
} catch (Throwable t) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
359 |
// Decrementing the count of current worker threads. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
360 |
// But, it will be increased in the finally block. |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
361 |
decrementCurrentNumberOfThreads(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
362 |
wrapper.workerThreadCreationFailure(t); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
363 |
} finally { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
364 |
incrementCurrentNumberOfThreads(); |
4 | 365 |
} |
366 |
} |
|
367 |
} |
|
368 |
||
369 |
public int minimumNumberOfThreads() { |
|
370 |
return minWorkerThreads; |
|
371 |
} |
|
372 |
||
373 |
public int maximumNumberOfThreads() { |
|
374 |
return maxWorkerThreads; |
|
375 |
} |
|
376 |
||
377 |
public long idleTimeoutForThreads() { |
|
378 |
return inactivityTimeout; |
|
379 |
} |
|
380 |
||
381 |
public int currentNumberOfThreads() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
382 |
synchronized (workQueue) { |
4 | 383 |
return currentThreadCount; |
384 |
} |
|
385 |
} |
|
386 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
387 |
void decrementCurrentNumberOfThreads() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
388 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
389 |
currentThreadCount--; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
390 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
391 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
392 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
393 |
void incrementCurrentNumberOfThreads() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
394 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
395 |
currentThreadCount++; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
396 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
397 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
398 |
|
4 | 399 |
public int numberOfAvailableThreads() { |
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
400 |
synchronized (workQueue) { |
4 | 401 |
return availableWorkerThreads; |
402 |
} |
|
403 |
} |
|
404 |
||
405 |
public int numberOfBusyThreads() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
406 |
synchronized (workQueue) { |
4 | 407 |
return (currentThreadCount - availableWorkerThreads); |
408 |
} |
|
409 |
} |
|
410 |
||
411 |
public long averageWorkCompletionTime() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
412 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
413 |
return (totalTimeTaken.get() / processedCount.get()); |
4 | 414 |
} |
415 |
} |
|
416 |
||
417 |
public long currentProcessedCount() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
418 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
419 |
return processedCount.get(); |
4 | 420 |
} |
421 |
} |
|
422 |
||
423 |
public String getName() { |
|
424 |
return name; |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* This method will return the number of WorkQueues serviced by the threadpool. |
|
429 |
*/ |
|
430 |
public int numberOfWorkQueues() { |
|
431 |
return 1; |
|
432 |
} |
|
433 |
||
434 |
||
435 |
private static synchronized int getUniqueThreadId() { |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
436 |
return ThreadPoolImpl.threadCounter.incrementAndGet(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
437 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
438 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
439 |
/** |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
440 |
* This method will decrement the number of available threads |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
441 |
* in the threadpool which are waiting for work. Called from |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
442 |
* WorkQueueImpl.requestWork() |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
443 |
*/ |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
444 |
void decrementNumberOfAvailableThreads() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
445 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
446 |
availableWorkerThreads--; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
447 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
448 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
449 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
450 |
/** |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
451 |
* This method will increment the number of available threads |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
452 |
* in the threadpool which are waiting for work. Called from |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
453 |
* WorkQueueImpl.requestWork() |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
454 |
*/ |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
455 |
void incrementNumberOfAvailableThreads() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
456 |
synchronized (workQueue) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
457 |
availableWorkerThreads++; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
458 |
} |
4 | 459 |
} |
460 |
||
461 |
||
33680
56aa0b79bf5a
8140606: Update library code to use internal Unsafe
chegar
parents:
29937
diff
changeset
|
462 |
private class WorkerThread extends sun.misc.ManagedLocalsThread implements Closeable |
4 | 463 |
{ |
464 |
private Work currentWork; |
|
465 |
private int threadId = 0; // unique id for the thread |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
466 |
private volatile boolean closeCalled = false; |
4 | 467 |
private String threadPoolName; |
468 |
// name seen by Thread.getName() |
|
469 |
private StringBuffer workerThreadName = new StringBuffer(); |
|
470 |
||
471 |
WorkerThread(ThreadGroup tg, String threadPoolName) { |
|
472 |
super(tg, "Idle"); |
|
473 |
this.threadId = ThreadPoolImpl.getUniqueThreadId(); |
|
474 |
this.threadPoolName = threadPoolName; |
|
475 |
setName(composeWorkerThreadName(threadPoolName, "Idle")); |
|
476 |
} |
|
477 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
478 |
public synchronized void close() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
479 |
closeCalled = true; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
480 |
interrupt(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
481 |
} |
4 | 482 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
483 |
private void resetClassLoader() { |
4 | 484 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
485 |
} |
4 | 486 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
487 |
private void performWork() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
488 |
long start = System.currentTimeMillis(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
489 |
try { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
490 |
currentWork.doWork(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
491 |
} catch (Throwable t) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
492 |
wrapper.workerThreadDoWorkThrowable(this, t); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
493 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
494 |
long elapsedTime = System.currentTimeMillis() - start; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
495 |
totalTimeTaken.addAndGet(elapsedTime); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
496 |
processedCount.incrementAndGet(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
497 |
} |
4 | 498 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
499 |
public void run() { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
500 |
try { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
501 |
while (!closeCalled) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
502 |
try { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
503 |
currentWork = ((WorkQueueImpl)workQueue).requestWork( |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
504 |
inactivityTimeout); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
505 |
if (currentWork == null) |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
506 |
continue; |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
507 |
} catch (InterruptedException exc) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
508 |
wrapper.workQueueThreadInterrupted( exc, getName(), |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
509 |
Boolean.valueOf(closeCalled)); |
4 | 510 |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
511 |
continue ; |
4 | 512 |
} catch (Throwable t) { |
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
513 |
wrapper.workerThreadThrowableFromRequestWork(this, t, |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
514 |
workQueue.getName()); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
515 |
|
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
516 |
continue; |
4 | 517 |
} |
518 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
519 |
performWork(); |
4 | 520 |
|
521 |
// set currentWork to null so that the work item can be |
|
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
522 |
// garbage collected without waiting for the next work item. |
4 | 523 |
currentWork = null; |
524 |
||
13171
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
525 |
resetClassLoader(); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
526 |
} |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
527 |
} catch (Throwable e) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
528 |
// This should not be possible |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
529 |
wrapper.workerThreadCaughtUnexpectedThrowable(this,e); |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
530 |
} finally { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
531 |
synchronized (workersLock) { |
1ac5e9a54a6e
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents:
5555
diff
changeset
|
532 |
workers.remove(this); |
4 | 533 |
} |
534 |
} |
|
535 |
} |
|
536 |
||
537 |
private String composeWorkerThreadName(String poolName, String workerName) { |
|
538 |
workerThreadName.setLength(0); |
|
539 |
workerThreadName.append("p: ").append(poolName); |
|
540 |
workerThreadName.append("; w: ").append(workerName); |
|
541 |
return workerThreadName.toString(); |
|
542 |
} |
|
543 |
} // End of WorkerThread class |
|
544 |
||
545 |
} |
|
546 |
||
547 |
// End of file. |