author | dl |
Thu, 07 Apr 2011 15:06:32 +0100 | |
changeset 9242 | ef138d47df58 |
parent 8765 | dfc2a131d08a |
child 11279 | d9dab5ec5044 |
permissions | -rw-r--r-- |
4110 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
4110 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8765
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
import java.io.Serializable; |
|
39 |
import java.util.Collection; |
|
40 |
import java.util.Collections; |
|
41 |
import java.util.List; |
|
42 |
import java.util.RandomAccess; |
|
43 |
import java.util.Map; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
44 |
import java.lang.ref.WeakReference; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
45 |
import java.lang.ref.ReferenceQueue; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
46 |
import java.util.concurrent.Callable; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
47 |
import java.util.concurrent.CancellationException; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
48 |
import java.util.concurrent.ExecutionException; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
49 |
import java.util.concurrent.Executor; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
50 |
import java.util.concurrent.ExecutorService; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
51 |
import java.util.concurrent.Future; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
52 |
import java.util.concurrent.RejectedExecutionException; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
53 |
import java.util.concurrent.RunnableFuture; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
54 |
import java.util.concurrent.TimeUnit; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
55 |
import java.util.concurrent.TimeoutException; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
56 |
import java.util.concurrent.locks.ReentrantLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
57 |
import java.lang.reflect.Constructor; |
4110 | 58 |
|
59 |
/** |
|
60 |
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
|
61 |
* A {@code ForkJoinTask} is a thread-like entity that is much |
|
62 |
* lighter weight than a normal thread. Huge numbers of tasks and |
|
63 |
* subtasks may be hosted by a small number of actual threads in a |
|
64 |
* ForkJoinPool, at the price of some usage limitations. |
|
65 |
* |
|
66 |
* <p>A "main" {@code ForkJoinTask} begins execution when submitted |
|
67 |
* to a {@link ForkJoinPool}. Once started, it will usually in turn |
|
68 |
* start other subtasks. As indicated by the name of this class, |
|
69 |
* many programs using {@code ForkJoinTask} employ only methods |
|
70 |
* {@link #fork} and {@link #join}, or derivatives such as {@link |
|
6674 | 71 |
* #invokeAll(ForkJoinTask...) invokeAll}. However, this class also |
72 |
* provides a number of other methods that can come into play in |
|
73 |
* advanced usages, as well as extension mechanics that allow |
|
74 |
* support of new forms of fork/join processing. |
|
4110 | 75 |
* |
76 |
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
|
77 |
* The efficiency of {@code ForkJoinTask}s stems from a set of |
|
78 |
* restrictions (that are only partially statically enforceable) |
|
79 |
* reflecting their intended use as computational tasks calculating |
|
80 |
* pure functions or operating on purely isolated objects. The |
|
81 |
* primary coordination mechanisms are {@link #fork}, that arranges |
|
82 |
* asynchronous execution, and {@link #join}, that doesn't proceed |
|
83 |
* until the task's result has been computed. Computations should |
|
84 |
* avoid {@code synchronized} methods or blocks, and should minimize |
|
85 |
* other blocking synchronization apart from joining other tasks or |
|
86 |
* using synchronizers such as Phasers that are advertised to |
|
87 |
* cooperate with fork/join scheduling. Tasks should also not perform |
|
88 |
* blocking IO, and should ideally access variables that are |
|
89 |
* completely independent of those accessed by other running |
|
90 |
* tasks. Minor breaches of these restrictions, for example using |
|
91 |
* shared output streams, may be tolerable in practice, but frequent |
|
92 |
* use may result in poor performance, and the potential to |
|
93 |
* indefinitely stall if the number of threads not waiting for IO or |
|
94 |
* other external synchronization becomes exhausted. This usage |
|
95 |
* restriction is in part enforced by not permitting checked |
|
96 |
* exceptions such as {@code IOExceptions} to be thrown. However, |
|
97 |
* computations may still encounter unchecked exceptions, that are |
|
98 |
* rethrown to callers attempting to join them. These exceptions may |
|
99 |
* additionally include {@link RejectedExecutionException} stemming |
|
100 |
* from internal resource exhaustion, such as failure to allocate |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
101 |
* internal task queues. Rethrown exceptions behave in the same way as |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
102 |
* regular exceptions, but, when possible, contain stack traces (as |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
103 |
* displayed for example using {@code ex.printStackTrace()}) of both |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
104 |
* the thread that initiated the computation as well as the thread |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
105 |
* actually encountering the exception; minimally only the latter. |
4110 | 106 |
* |
107 |
* <p>The primary method for awaiting completion and extracting |
|
108 |
* results of a task is {@link #join}, but there are several variants: |
|
109 |
* The {@link Future#get} methods support interruptible and/or timed |
|
110 |
* waits for completion and report results using {@code Future} |
|
6543 | 111 |
* conventions. Method {@link #invoke} is semantically |
4110 | 112 |
* equivalent to {@code fork(); join()} but always attempts to begin |
113 |
* execution in the current thread. The "<em>quiet</em>" forms of |
|
114 |
* these methods do not extract results or report exceptions. These |
|
115 |
* may be useful when a set of tasks are being executed, and you need |
|
116 |
* to delay processing of results or exceptions until all complete. |
|
117 |
* Method {@code invokeAll} (available in multiple versions) |
|
118 |
* performs the most common form of parallel invocation: forking a set |
|
119 |
* of tasks and joining them all. |
|
120 |
* |
|
121 |
* <p>The execution status of tasks may be queried at several levels |
|
122 |
* of detail: {@link #isDone} is true if a task completed in any way |
|
123 |
* (including the case where a task was cancelled without executing); |
|
124 |
* {@link #isCompletedNormally} is true if a task completed without |
|
125 |
* cancellation or encountering an exception; {@link #isCancelled} is |
|
126 |
* true if the task was cancelled (in which case {@link #getException} |
|
127 |
* returns a {@link java.util.concurrent.CancellationException}); and |
|
128 |
* {@link #isCompletedAbnormally} is true if a task was either |
|
129 |
* cancelled or encountered an exception, in which case {@link |
|
130 |
* #getException} will return either the encountered exception or |
|
131 |
* {@link java.util.concurrent.CancellationException}. |
|
132 |
* |
|
133 |
* <p>The ForkJoinTask class is not usually directly subclassed. |
|
134 |
* Instead, you subclass one of the abstract classes that support a |
|
135 |
* particular style of fork/join processing, typically {@link |
|
136 |
* RecursiveAction} for computations that do not return results, or |
|
137 |
* {@link RecursiveTask} for those that do. Normally, a concrete |
|
138 |
* ForkJoinTask subclass declares fields comprising its parameters, |
|
139 |
* established in a constructor, and then defines a {@code compute} |
|
140 |
* method that somehow uses the control methods supplied by this base |
|
141 |
* class. While these methods have {@code public} access (to allow |
|
142 |
* instances of different task subclasses to call each other's |
|
143 |
* methods), some of them may only be called from within other |
|
144 |
* ForkJoinTasks (as may be determined using method {@link |
|
145 |
* #inForkJoinPool}). Attempts to invoke them in other contexts |
|
146 |
* result in exceptions or errors, possibly including |
|
6543 | 147 |
* {@code ClassCastException}. |
4110 | 148 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
149 |
* <p>Method {@link #join} and its variants are appropriate for use |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
150 |
* only when completion dependencies are acyclic; that is, the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
151 |
* parallel computation can be described as a directed acyclic graph |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
152 |
* (DAG). Otherwise, executions may encounter a form of deadlock as |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
153 |
* tasks cyclically wait for each other. However, this framework |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
154 |
* supports other methods and techniques (for example the use of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
155 |
* {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
156 |
* may be of use in constructing custom subclasses for problems that |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
157 |
* are not statically structured as DAGs. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
158 |
* |
4110 | 159 |
* <p>Most base support methods are {@code final}, to prevent |
160 |
* overriding of implementations that are intrinsically tied to the |
|
161 |
* underlying lightweight task scheduling framework. Developers |
|
162 |
* creating new basic styles of fork/join processing should minimally |
|
163 |
* implement {@code protected} methods {@link #exec}, {@link |
|
164 |
* #setRawResult}, and {@link #getRawResult}, while also introducing |
|
165 |
* an abstract computational method that can be implemented in its |
|
166 |
* subclasses, possibly relying on other {@code protected} methods |
|
167 |
* provided by this class. |
|
168 |
* |
|
169 |
* <p>ForkJoinTasks should perform relatively small amounts of |
|
170 |
* computation. Large tasks should be split into smaller subtasks, |
|
171 |
* usually via recursive decomposition. As a very rough rule of thumb, |
|
172 |
* a task should perform more than 100 and less than 10000 basic |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
173 |
* computational steps, and should avoid indefinite looping. If tasks |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
174 |
* are too big, then parallelism cannot improve throughput. If too |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
175 |
* small, then memory and internal task maintenance overhead may |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
176 |
* overwhelm processing. |
4110 | 177 |
* |
178 |
* <p>This class provides {@code adapt} methods for {@link Runnable} |
|
179 |
* and {@link Callable}, that may be of use when mixing execution of |
|
6543 | 180 |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks are |
181 |
* of this form, consider using a pool constructed in <em>asyncMode</em>. |
|
4110 | 182 |
* |
183 |
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
|
184 |
* used in extensions such as remote execution frameworks. It is |
|
185 |
* sensible to serialize tasks only before or after, but not during, |
|
186 |
* execution. Serialization is not relied on during execution itself. |
|
187 |
* |
|
188 |
* @since 1.7 |
|
189 |
* @author Doug Lea |
|
190 |
*/ |
|
191 |
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
|
192 |
||
6543 | 193 |
/* |
194 |
* See the internal documentation of class ForkJoinPool for a |
|
195 |
* general implementation overview. ForkJoinTasks are mainly |
|
196 |
* responsible for maintaining their "status" field amidst relays |
|
197 |
* to methods in ForkJoinWorkerThread and ForkJoinPool. The |
|
198 |
* methods of this class are more-or-less layered into (1) basic |
|
199 |
* status maintenance (2) execution and awaiting completion (3) |
|
200 |
* user-level methods that additionally report results. This is |
|
201 |
* sometimes hard to see because this file orders exported methods |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
202 |
* in a way that flows well in javadocs. |
4110 | 203 |
*/ |
6543 | 204 |
|
205 |
/* |
|
206 |
* The status field holds run control status bits packed into a |
|
207 |
* single int to minimize footprint and to ensure atomicity (via |
|
208 |
* CAS). Status is initially zero, and takes on nonnegative |
|
209 |
* values until completed, upon which status holds value |
|
210 |
* NORMAL, CANCELLED, or EXCEPTIONAL. Tasks undergoing blocking |
|
211 |
* waits by other threads have the SIGNAL bit set. Completion of |
|
212 |
* a stolen task with SIGNAL set awakens any waiters via |
|
213 |
* notifyAll. Even though suboptimal for some purposes, we use |
|
214 |
* basic builtin wait/notify to take advantage of "monitor |
|
215 |
* inflation" in JVMs that we would otherwise need to emulate to |
|
216 |
* avoid adding further per-task bookkeeping overhead. We want |
|
217 |
* these monitors to be "fat", i.e., not use biasing or thin-lock |
|
218 |
* techniques, so use some odd coding idioms that tend to avoid |
|
219 |
* them. |
|
220 |
*/ |
|
221 |
||
222 |
/** The run status of this task */ |
|
4110 | 223 |
volatile int status; // accessed directly by pool and workers |
6543 | 224 |
private static final int NORMAL = -1; |
225 |
private static final int CANCELLED = -2; |
|
226 |
private static final int EXCEPTIONAL = -3; |
|
227 |
private static final int SIGNAL = 1; |
|
4110 | 228 |
|
229 |
/** |
|
6543 | 230 |
* Marks completion and wakes up threads waiting to join this task, |
231 |
* also clearing signal request bits. |
|
4110 | 232 |
* |
233 |
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
234 |
* @return completion status on exit |
4110 | 235 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
236 |
private int setCompletion(int completion) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
237 |
for (int s;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
238 |
if ((s = status) < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
239 |
return s; |
6543 | 240 |
if (UNSAFE.compareAndSwapInt(this, statusOffset, s, completion)) { |
241 |
if (s != 0) |
|
242 |
synchronized (this) { notifyAll(); } |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
243 |
return completion; |
6543 | 244 |
} |
4110 | 245 |
} |
246 |
} |
|
247 |
||
248 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
249 |
* Tries to block a worker thread until completed or timed out. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
250 |
* Uses Object.wait time argument conventions. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
251 |
* May fail on contention or interrupt. |
6543 | 252 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
253 |
* @param millis if > 0, wait time. |
4110 | 254 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
255 |
final void tryAwaitDone(long millis) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
256 |
int s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
257 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
258 |
if (((s = status) > 0 || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
259 |
(s == 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
260 |
UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL))) && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
261 |
status > 0) { |
6674 | 262 |
synchronized (this) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
263 |
if (status > 0) |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
264 |
wait(millis); |
4110 | 265 |
} |
266 |
} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
267 |
} catch (InterruptedException ie) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
268 |
// caller must check termination |
4110 | 269 |
} |
270 |
} |
|
271 |
||
272 |
/** |
|
6543 | 273 |
* Blocks a non-worker-thread until completion. |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
274 |
* @return status upon completion |
4110 | 275 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
276 |
private int externalAwaitDone() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
277 |
int s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
278 |
if ((s = status) >= 0) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
279 |
boolean interrupted = false; |
6674 | 280 |
synchronized (this) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
281 |
while ((s = status) >= 0) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
282 |
if (s == 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
283 |
UNSAFE.compareAndSwapInt(this, statusOffset, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
284 |
0, SIGNAL); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
285 |
else { |
6543 | 286 |
try { |
287 |
wait(); |
|
288 |
} catch (InterruptedException ie) { |
|
289 |
interrupted = true; |
|
290 |
} |
|
291 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
292 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
293 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
294 |
if (interrupted) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
295 |
Thread.currentThread().interrupt(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
296 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
297 |
return s; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
298 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
299 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
300 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
301 |
* Blocks a non-worker-thread until completion or interruption or timeout. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
302 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
303 |
private int externalInterruptibleAwaitDone(long millis) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
304 |
throws InterruptedException { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
305 |
int s; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
306 |
if (Thread.interrupted()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
307 |
throw new InterruptedException(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
308 |
if ((s = status) >= 0) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
309 |
synchronized (this) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
310 |
while ((s = status) >= 0) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
311 |
if (s == 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
312 |
UNSAFE.compareAndSwapInt(this, statusOffset, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
313 |
0, SIGNAL); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
314 |
else { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
315 |
wait(millis); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
316 |
if (millis > 0L) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
317 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
318 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
319 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
320 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
321 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
322 |
return s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
323 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
324 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
325 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
326 |
* Primary execution method for stolen tasks. Unless done, calls |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
327 |
* exec and records status if completed, but doesn't wait for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
328 |
* completion otherwise. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
329 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
330 |
final void doExec() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
331 |
if (status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
332 |
boolean completed; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
333 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
334 |
completed = exec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
335 |
} catch (Throwable rex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
336 |
setExceptionalCompletion(rex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
337 |
return; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
338 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
339 |
if (completed) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
340 |
setCompletion(NORMAL); // must be outside try block |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
341 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
342 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
343 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
344 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
345 |
* Primary mechanics for join, get, quietlyJoin. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
346 |
* @return status upon completion |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
347 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
348 |
private int doJoin() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
349 |
Thread t; ForkJoinWorkerThread w; int s; boolean completed; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
350 |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
351 |
if ((s = status) < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
352 |
return s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
353 |
if ((w = (ForkJoinWorkerThread)t).unpushTask(this)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
354 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
355 |
completed = exec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
356 |
} catch (Throwable rex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
357 |
return setExceptionalCompletion(rex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
358 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
359 |
if (completed) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
360 |
return setCompletion(NORMAL); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
361 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
362 |
return w.joinTask(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
363 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
364 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
365 |
return externalAwaitDone(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
366 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
367 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
368 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
369 |
* Primary mechanics for invoke, quietlyInvoke. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
370 |
* @return status upon completion |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
371 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
372 |
private int doInvoke() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
373 |
int s; boolean completed; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
374 |
if ((s = status) < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
375 |
return s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
376 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
377 |
completed = exec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
378 |
} catch (Throwable rex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
379 |
return setExceptionalCompletion(rex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
380 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
381 |
if (completed) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
382 |
return setCompletion(NORMAL); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
383 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
384 |
return doJoin(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
385 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
386 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
387 |
// Exception table support |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
388 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
389 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
390 |
* Table of exceptions thrown by tasks, to enable reporting by |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
391 |
* callers. Because exceptions are rare, we don't directly keep |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
392 |
* them with task objects, but instead use a weak ref table. Note |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
393 |
* that cancellation exceptions don't appear in the table, but are |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
394 |
* instead recorded as status values. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
395 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
396 |
* Note: These statics are initialized below in static block. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
397 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
398 |
private static final ExceptionNode[] exceptionTable; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
399 |
private static final ReentrantLock exceptionTableLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
400 |
private static final ReferenceQueue<Object> exceptionTableRefQueue; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
401 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
402 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
403 |
* Fixed capacity for exceptionTable. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
404 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
405 |
private static final int EXCEPTION_MAP_CAPACITY = 32; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
406 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
407 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
408 |
* Key-value nodes for exception table. The chained hash table |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
409 |
* uses identity comparisons, full locking, and weak references |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
410 |
* for keys. The table has a fixed capacity because it only |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
411 |
* maintains task exceptions long enough for joiners to access |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
412 |
* them, so should never become very large for sustained |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
413 |
* periods. However, since we do not know when the last joiner |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
414 |
* completes, we must use weak references and expunge them. We do |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
415 |
* so on each operation (hence full locking). Also, some thread in |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
416 |
* any ForkJoinPool will call helpExpungeStaleExceptions when its |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
417 |
* pool becomes isQuiescent. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
418 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
419 |
static final class ExceptionNode extends WeakReference<ForkJoinTask<?>>{ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
420 |
final Throwable ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
421 |
ExceptionNode next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
422 |
final long thrower; // use id not ref to avoid weak cycles |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
423 |
ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
424 |
super(task, exceptionTableRefQueue); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
425 |
this.ex = ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
426 |
this.next = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
427 |
this.thrower = Thread.currentThread().getId(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
428 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
429 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
430 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
431 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
432 |
* Records exception and sets exceptional completion. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
433 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
434 |
* @return status on exit |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
435 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
436 |
private int setExceptionalCompletion(Throwable ex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
437 |
int h = System.identityHashCode(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
438 |
final ReentrantLock lock = exceptionTableLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
439 |
lock.lock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
440 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
441 |
expungeStaleExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
442 |
ExceptionNode[] t = exceptionTable; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
443 |
int i = h & (t.length - 1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
444 |
for (ExceptionNode e = t[i]; ; e = e.next) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
445 |
if (e == null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
446 |
t[i] = new ExceptionNode(this, ex, t[i]); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
447 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
448 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
449 |
if (e.get() == this) // already present |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
450 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
451 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
452 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
453 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
454 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
455 |
return setCompletion(EXCEPTIONAL); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
456 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
457 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
458 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
459 |
* Removes exception node and clears status |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
460 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
461 |
private void clearExceptionalCompletion() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
462 |
int h = System.identityHashCode(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
463 |
final ReentrantLock lock = exceptionTableLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
464 |
lock.lock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
465 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
466 |
ExceptionNode[] t = exceptionTable; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
467 |
int i = h & (t.length - 1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
468 |
ExceptionNode e = t[i]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
469 |
ExceptionNode pred = null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
470 |
while (e != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
471 |
ExceptionNode next = e.next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
472 |
if (e.get() == this) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
473 |
if (pred == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
474 |
t[i] = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
475 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
476 |
pred.next = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
477 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
478 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
479 |
pred = e; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
480 |
e = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
481 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
482 |
expungeStaleExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
483 |
status = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
484 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
485 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
486 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
487 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
488 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
489 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
490 |
* Returns a rethrowable exception for the given task, if |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
491 |
* available. To provide accurate stack traces, if the exception |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
492 |
* was not thrown by the current thread, we try to create a new |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
493 |
* exception of the same type as the one thrown, but with the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
494 |
* recorded exception as its cause. If there is no such |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
495 |
* constructor, we instead try to use a no-arg constructor, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
496 |
* followed by initCause, to the same effect. If none of these |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
497 |
* apply, or any fail due to other exceptions, we return the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
498 |
* recorded exception, which is still correct, although it may |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
499 |
* contain a misleading stack trace. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
500 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
501 |
* @return the exception, or null if none |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
502 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
503 |
private Throwable getThrowableException() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
504 |
if (status != EXCEPTIONAL) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
505 |
return null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
506 |
int h = System.identityHashCode(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
507 |
ExceptionNode e; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
508 |
final ReentrantLock lock = exceptionTableLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
509 |
lock.lock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
510 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
511 |
expungeStaleExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
512 |
ExceptionNode[] t = exceptionTable; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
513 |
e = t[h & (t.length - 1)]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
514 |
while (e != null && e.get() != this) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
515 |
e = e.next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
516 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
517 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
518 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
519 |
Throwable ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
520 |
if (e == null || (ex = e.ex) == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
521 |
return null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
522 |
if (e.thrower != Thread.currentThread().getId()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
523 |
Class ec = ex.getClass(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
524 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
525 |
Constructor<?> noArgCtor = null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
526 |
Constructor<?>[] cs = ec.getConstructors();// public ctors only |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
527 |
for (int i = 0; i < cs.length; ++i) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
528 |
Constructor<?> c = cs[i]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
529 |
Class<?>[] ps = c.getParameterTypes(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
530 |
if (ps.length == 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
531 |
noArgCtor = c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
532 |
else if (ps.length == 1 && ps[0] == Throwable.class) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
533 |
return (Throwable)(c.newInstance(ex)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
534 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
535 |
if (noArgCtor != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
536 |
Throwable wx = (Throwable)(noArgCtor.newInstance()); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
537 |
wx.initCause(ex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
538 |
return wx; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
539 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
540 |
} catch (Exception ignore) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
541 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
542 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
543 |
return ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
544 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
545 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
546 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
547 |
* Poll stale refs and remove them. Call only while holding lock. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
548 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
549 |
private static void expungeStaleExceptions() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
550 |
for (Object x; (x = exceptionTableRefQueue.poll()) != null;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
551 |
if (x instanceof ExceptionNode) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
552 |
ForkJoinTask<?> key = ((ExceptionNode)x).get(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
553 |
ExceptionNode[] t = exceptionTable; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
554 |
int i = System.identityHashCode(key) & (t.length - 1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
555 |
ExceptionNode e = t[i]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
556 |
ExceptionNode pred = null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
557 |
while (e != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
558 |
ExceptionNode next = e.next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
559 |
if (e == x) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
560 |
if (pred == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
561 |
t[i] = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
562 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
563 |
pred.next = next; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
564 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
565 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
566 |
pred = e; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
567 |
e = next; |
4110 | 568 |
} |
569 |
} |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
574 |
* If lock is available, poll stale refs and remove them. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
575 |
* Called from ForkJoinPool when pools become quiescent. |
4110 | 576 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
577 |
static final void helpExpungeStaleExceptions() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
578 |
final ReentrantLock lock = exceptionTableLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
579 |
if (lock.tryLock()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
580 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
581 |
expungeStaleExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
582 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
583 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
584 |
} |
4110 | 585 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
586 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
587 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
588 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
589 |
* Report the result of invoke or join; called only upon |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
590 |
* non-normal return of internal versions. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
591 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
592 |
private V reportResult() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
593 |
int s; Throwable ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
594 |
if ((s = status) == CANCELLED) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
595 |
throw new CancellationException(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
596 |
if (s == EXCEPTIONAL && (ex = getThrowableException()) != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
597 |
UNSAFE.throwException(ex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
598 |
return getRawResult(); |
4110 | 599 |
} |
600 |
||
601 |
// public methods |
|
602 |
||
603 |
/** |
|
604 |
* Arranges to asynchronously execute this task. While it is not |
|
605 |
* necessarily enforced, it is a usage error to fork a task more |
|
606 |
* than once unless it has completed and been reinitialized. |
|
607 |
* Subsequent modifications to the state of this task or any data |
|
608 |
* it operates on are not necessarily consistently observable by |
|
609 |
* any thread other than the one executing it unless preceded by a |
|
610 |
* call to {@link #join} or related methods, or a call to {@link |
|
611 |
* #isDone} returning {@code true}. |
|
612 |
* |
|
613 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
614 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 615 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
616 |
* result in exceptions or errors, possibly including {@code |
|
617 |
* ClassCastException}. |
|
618 |
* |
|
619 |
* @return {@code this}, to simplify usage |
|
620 |
*/ |
|
621 |
public final ForkJoinTask<V> fork() { |
|
622 |
((ForkJoinWorkerThread) Thread.currentThread()) |
|
623 |
.pushTask(this); |
|
624 |
return this; |
|
625 |
} |
|
626 |
||
627 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
628 |
* Returns the result of the computation when it {@link #isDone is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
629 |
* done}. This method differs from {@link #get()} in that |
4110 | 630 |
* abnormal completion results in {@code RuntimeException} or |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
631 |
* {@code Error}, not {@code ExecutionException}, and that |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
632 |
* interrupts of the calling thread do <em>not</em> cause the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
633 |
* method to abruptly return by throwing {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
634 |
* InterruptedException}. |
4110 | 635 |
* |
636 |
* @return the computed result |
|
637 |
*/ |
|
638 |
public final V join() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
639 |
if (doJoin() != NORMAL) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
640 |
return reportResult(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
641 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
642 |
return getRawResult(); |
4110 | 643 |
} |
644 |
||
645 |
/** |
|
646 |
* Commences performing this task, awaits its completion if |
|
6543 | 647 |
* necessary, and returns its result, or throws an (unchecked) |
648 |
* {@code RuntimeException} or {@code Error} if the underlying |
|
649 |
* computation did so. |
|
4110 | 650 |
* |
651 |
* @return the computed result |
|
652 |
*/ |
|
653 |
public final V invoke() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
654 |
if (doInvoke() != NORMAL) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
655 |
return reportResult(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
656 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
657 |
return getRawResult(); |
4110 | 658 |
} |
659 |
||
660 |
/** |
|
661 |
* Forks the given tasks, returning when {@code isDone} holds for |
|
662 |
* each task or an (unchecked) exception is encountered, in which |
|
6543 | 663 |
* case the exception is rethrown. If more than one task |
664 |
* encounters an exception, then this method throws any one of |
|
665 |
* these exceptions. If any task encounters an exception, the |
|
666 |
* other may be cancelled. However, the execution status of |
|
667 |
* individual tasks is not guaranteed upon exceptional return. The |
|
668 |
* status of each task may be obtained using {@link |
|
669 |
* #getException()} and related methods to check if they have been |
|
670 |
* cancelled, completed normally or exceptionally, or left |
|
671 |
* unprocessed. |
|
4110 | 672 |
* |
673 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
674 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 675 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
676 |
* result in exceptions or errors, possibly including {@code |
|
677 |
* ClassCastException}. |
|
678 |
* |
|
679 |
* @param t1 the first task |
|
680 |
* @param t2 the second task |
|
681 |
* @throws NullPointerException if any task is null |
|
682 |
*/ |
|
683 |
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) { |
|
684 |
t2.fork(); |
|
685 |
t1.invoke(); |
|
686 |
t2.join(); |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Forks the given tasks, returning when {@code isDone} holds for |
|
691 |
* each task or an (unchecked) exception is encountered, in which |
|
6543 | 692 |
* case the exception is rethrown. If more than one task |
693 |
* encounters an exception, then this method throws any one of |
|
694 |
* these exceptions. If any task encounters an exception, others |
|
695 |
* may be cancelled. However, the execution status of individual |
|
696 |
* tasks is not guaranteed upon exceptional return. The status of |
|
697 |
* each task may be obtained using {@link #getException()} and |
|
698 |
* related methods to check if they have been cancelled, completed |
|
699 |
* normally or exceptionally, or left unprocessed. |
|
4110 | 700 |
* |
701 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
702 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 703 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
704 |
* result in exceptions or errors, possibly including {@code |
|
705 |
* ClassCastException}. |
|
706 |
* |
|
707 |
* @param tasks the tasks |
|
708 |
* @throws NullPointerException if any task is null |
|
709 |
*/ |
|
710 |
public static void invokeAll(ForkJoinTask<?>... tasks) { |
|
711 |
Throwable ex = null; |
|
712 |
int last = tasks.length - 1; |
|
713 |
for (int i = last; i >= 0; --i) { |
|
714 |
ForkJoinTask<?> t = tasks[i]; |
|
715 |
if (t == null) { |
|
716 |
if (ex == null) |
|
717 |
ex = new NullPointerException(); |
|
718 |
} |
|
719 |
else if (i != 0) |
|
720 |
t.fork(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
721 |
else if (t.doInvoke() < NORMAL && ex == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
722 |
ex = t.getException(); |
4110 | 723 |
} |
724 |
for (int i = 1; i <= last; ++i) { |
|
725 |
ForkJoinTask<?> t = tasks[i]; |
|
726 |
if (t != null) { |
|
727 |
if (ex != null) |
|
728 |
t.cancel(false); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
729 |
else if (t.doJoin() < NORMAL && ex == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
730 |
ex = t.getException(); |
4110 | 731 |
} |
732 |
} |
|
733 |
if (ex != null) |
|
6543 | 734 |
UNSAFE.throwException(ex); |
4110 | 735 |
} |
736 |
||
737 |
/** |
|
738 |
* Forks all tasks in the specified collection, returning when |
|
739 |
* {@code isDone} holds for each task or an (unchecked) exception |
|
6543 | 740 |
* is encountered, in which case the exception is rethrown. If |
741 |
* more than one task encounters an exception, then this method |
|
742 |
* throws any one of these exceptions. If any task encounters an |
|
743 |
* exception, others may be cancelled. However, the execution |
|
744 |
* status of individual tasks is not guaranteed upon exceptional |
|
745 |
* return. The status of each task may be obtained using {@link |
|
746 |
* #getException()} and related methods to check if they have been |
|
747 |
* cancelled, completed normally or exceptionally, or left |
|
748 |
* unprocessed. |
|
4110 | 749 |
* |
750 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
751 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 752 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
753 |
* result in exceptions or errors, possibly including {@code |
|
754 |
* ClassCastException}. |
|
755 |
* |
|
756 |
* @param tasks the collection of tasks |
|
757 |
* @return the tasks argument, to simplify usage |
|
758 |
* @throws NullPointerException if tasks or any element are null |
|
759 |
*/ |
|
760 |
public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) { |
|
761 |
if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) { |
|
762 |
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
|
763 |
return tasks; |
|
764 |
} |
|
765 |
@SuppressWarnings("unchecked") |
|
766 |
List<? extends ForkJoinTask<?>> ts = |
|
767 |
(List<? extends ForkJoinTask<?>>) tasks; |
|
768 |
Throwable ex = null; |
|
769 |
int last = ts.size() - 1; |
|
770 |
for (int i = last; i >= 0; --i) { |
|
771 |
ForkJoinTask<?> t = ts.get(i); |
|
772 |
if (t == null) { |
|
773 |
if (ex == null) |
|
774 |
ex = new NullPointerException(); |
|
775 |
} |
|
776 |
else if (i != 0) |
|
777 |
t.fork(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
778 |
else if (t.doInvoke() < NORMAL && ex == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
779 |
ex = t.getException(); |
4110 | 780 |
} |
781 |
for (int i = 1; i <= last; ++i) { |
|
782 |
ForkJoinTask<?> t = ts.get(i); |
|
783 |
if (t != null) { |
|
784 |
if (ex != null) |
|
785 |
t.cancel(false); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
786 |
else if (t.doJoin() < NORMAL && ex == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
787 |
ex = t.getException(); |
4110 | 788 |
} |
789 |
} |
|
790 |
if (ex != null) |
|
6543 | 791 |
UNSAFE.throwException(ex); |
4110 | 792 |
return tasks; |
793 |
} |
|
794 |
||
795 |
/** |
|
796 |
* Attempts to cancel execution of this task. This attempt will |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
797 |
* fail if the task has already completed or could not be |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
798 |
* cancelled for some other reason. If successful, and this task |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
799 |
* has not started when {@code cancel} is called, execution of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
800 |
* this task is suppressed. After this method returns |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
801 |
* successfully, unless there is an intervening call to {@link |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
802 |
* #reinitialize}, subsequent calls to {@link #isCancelled}, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
803 |
* {@link #isDone}, and {@code cancel} will return {@code true} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
804 |
* and calls to {@link #join} and related methods will result in |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
805 |
* {@code CancellationException}. |
4110 | 806 |
* |
807 |
* <p>This method may be overridden in subclasses, but if so, must |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
808 |
* still ensure that these properties hold. In particular, the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
809 |
* {@code cancel} method itself must not throw exceptions. |
4110 | 810 |
* |
811 |
* <p>This method is designed to be invoked by <em>other</em> |
|
812 |
* tasks. To terminate the current task, you can just return or |
|
813 |
* throw an unchecked exception from its computation method, or |
|
814 |
* invoke {@link #completeExceptionally}. |
|
815 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
816 |
* @param mayInterruptIfRunning this value has no effect in the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
817 |
* default implementation because interrupts are not used to |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
818 |
* control cancellation. |
4110 | 819 |
* |
820 |
* @return {@code true} if this task is now cancelled |
|
821 |
*/ |
|
822 |
public boolean cancel(boolean mayInterruptIfRunning) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
823 |
return setCompletion(CANCELLED) == CANCELLED; |
6543 | 824 |
} |
825 |
||
826 |
/** |
|
827 |
* Cancels, ignoring any exceptions thrown by cancel. Used during |
|
828 |
* worker and pool shutdown. Cancel is spec'ed not to throw any |
|
829 |
* exceptions, but if it does anyway, we have no recourse during |
|
830 |
* shutdown, so guard against this case. |
|
831 |
*/ |
|
832 |
final void cancelIgnoringExceptions() { |
|
833 |
try { |
|
834 |
cancel(false); |
|
835 |
} catch (Throwable ignore) { |
|
836 |
} |
|
837 |
} |
|
838 |
||
4110 | 839 |
public final boolean isDone() { |
840 |
return status < 0; |
|
841 |
} |
|
842 |
||
843 |
public final boolean isCancelled() { |
|
6543 | 844 |
return status == CANCELLED; |
4110 | 845 |
} |
846 |
||
847 |
/** |
|
848 |
* Returns {@code true} if this task threw an exception or was cancelled. |
|
849 |
* |
|
850 |
* @return {@code true} if this task threw an exception or was cancelled |
|
851 |
*/ |
|
852 |
public final boolean isCompletedAbnormally() { |
|
6543 | 853 |
return status < NORMAL; |
4110 | 854 |
} |
855 |
||
856 |
/** |
|
857 |
* Returns {@code true} if this task completed without throwing an |
|
858 |
* exception and was not cancelled. |
|
859 |
* |
|
860 |
* @return {@code true} if this task completed without throwing an |
|
861 |
* exception and was not cancelled |
|
862 |
*/ |
|
863 |
public final boolean isCompletedNormally() { |
|
6543 | 864 |
return status == NORMAL; |
4110 | 865 |
} |
866 |
||
867 |
/** |
|
868 |
* Returns the exception thrown by the base computation, or a |
|
869 |
* {@code CancellationException} if cancelled, or {@code null} if |
|
870 |
* none or if the method has not yet completed. |
|
871 |
* |
|
872 |
* @return the exception, or {@code null} if none |
|
873 |
*/ |
|
874 |
public final Throwable getException() { |
|
6543 | 875 |
int s = status; |
4110 | 876 |
return ((s >= NORMAL) ? null : |
877 |
(s == CANCELLED) ? new CancellationException() : |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
878 |
getThrowableException()); |
4110 | 879 |
} |
880 |
||
881 |
/** |
|
882 |
* Completes this task abnormally, and if not already aborted or |
|
883 |
* cancelled, causes it to throw the given exception upon |
|
884 |
* {@code join} and related operations. This method may be used |
|
885 |
* to induce exceptions in asynchronous tasks, or to force |
|
886 |
* completion of tasks that would not otherwise complete. Its use |
|
887 |
* in other situations is discouraged. This method is |
|
888 |
* overridable, but overridden versions must invoke {@code super} |
|
889 |
* implementation to maintain guarantees. |
|
890 |
* |
|
891 |
* @param ex the exception to throw. If this exception is not a |
|
892 |
* {@code RuntimeException} or {@code Error}, the actual exception |
|
893 |
* thrown will be a {@code RuntimeException} with cause {@code ex}. |
|
894 |
*/ |
|
895 |
public void completeExceptionally(Throwable ex) { |
|
6543 | 896 |
setExceptionalCompletion((ex instanceof RuntimeException) || |
897 |
(ex instanceof Error) ? ex : |
|
898 |
new RuntimeException(ex)); |
|
4110 | 899 |
} |
900 |
||
901 |
/** |
|
902 |
* Completes this task, and if not already aborted or cancelled, |
|
6543 | 903 |
* returning the given value as the result of subsequent |
904 |
* invocations of {@code join} and related operations. This method |
|
905 |
* may be used to provide results for asynchronous tasks, or to |
|
906 |
* provide alternative handling for tasks that would not otherwise |
|
907 |
* complete normally. Its use in other situations is |
|
908 |
* discouraged. This method is overridable, but overridden |
|
909 |
* versions must invoke {@code super} implementation to maintain |
|
910 |
* guarantees. |
|
4110 | 911 |
* |
912 |
* @param value the result value for this task |
|
913 |
*/ |
|
914 |
public void complete(V value) { |
|
915 |
try { |
|
916 |
setRawResult(value); |
|
917 |
} catch (Throwable rex) { |
|
6543 | 918 |
setExceptionalCompletion(rex); |
4110 | 919 |
return; |
920 |
} |
|
6543 | 921 |
setCompletion(NORMAL); |
4110 | 922 |
} |
923 |
||
6674 | 924 |
/** |
925 |
* Waits if necessary for the computation to complete, and then |
|
926 |
* retrieves its result. |
|
927 |
* |
|
928 |
* @return the computed result |
|
929 |
* @throws CancellationException if the computation was cancelled |
|
930 |
* @throws ExecutionException if the computation threw an |
|
931 |
* exception |
|
932 |
* @throws InterruptedException if the current thread is not a |
|
933 |
* member of a ForkJoinPool and was interrupted while waiting |
|
934 |
*/ |
|
4110 | 935 |
public final V get() throws InterruptedException, ExecutionException { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
936 |
int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ? |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
937 |
doJoin() : externalInterruptibleAwaitDone(0L); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
938 |
Throwable ex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
939 |
if (s == CANCELLED) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
940 |
throw new CancellationException(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
941 |
if (s == EXCEPTIONAL && (ex = getThrowableException()) != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
942 |
throw new ExecutionException(ex); |
6543 | 943 |
return getRawResult(); |
4110 | 944 |
} |
945 |
||
6674 | 946 |
/** |
947 |
* Waits if necessary for at most the given time for the computation |
|
948 |
* to complete, and then retrieves its result, if available. |
|
949 |
* |
|
950 |
* @param timeout the maximum time to wait |
|
951 |
* @param unit the time unit of the timeout argument |
|
952 |
* @return the computed result |
|
953 |
* @throws CancellationException if the computation was cancelled |
|
954 |
* @throws ExecutionException if the computation threw an |
|
955 |
* exception |
|
956 |
* @throws InterruptedException if the current thread is not a |
|
957 |
* member of a ForkJoinPool and was interrupted while waiting |
|
958 |
* @throws TimeoutException if the wait timed out |
|
959 |
*/ |
|
4110 | 960 |
public final V get(long timeout, TimeUnit unit) |
961 |
throws InterruptedException, ExecutionException, TimeoutException { |
|
6543 | 962 |
Thread t = Thread.currentThread(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
963 |
if (t instanceof ForkJoinWorkerThread) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
964 |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
965 |
long nanos = unit.toNanos(timeout); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
966 |
if (status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
967 |
boolean completed = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
968 |
if (w.unpushTask(this)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
969 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
970 |
completed = exec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
971 |
} catch (Throwable rex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
972 |
setExceptionalCompletion(rex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
973 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
974 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
975 |
if (completed) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
976 |
setCompletion(NORMAL); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
977 |
else if (status >= 0 && nanos > 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
978 |
w.pool.timedAwaitJoin(this, nanos); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
979 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
980 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
981 |
else { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
982 |
long millis = unit.toMillis(timeout); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
983 |
if (millis > 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
984 |
externalInterruptibleAwaitDone(millis); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
985 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
986 |
int s = status; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
987 |
if (s != NORMAL) { |
6543 | 988 |
Throwable ex; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
989 |
if (s == CANCELLED) |
6543 | 990 |
throw new CancellationException(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
991 |
if (s != EXCEPTIONAL) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
992 |
throw new TimeoutException(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
993 |
if ((ex = getThrowableException()) != null) |
6543 | 994 |
throw new ExecutionException(ex); |
995 |
} |
|
4110 | 996 |
return getRawResult(); |
997 |
} |
|
998 |
||
999 |
/** |
|
6543 | 1000 |
* Joins this task, without returning its result or throwing its |
4110 | 1001 |
* exception. This method may be useful when processing |
1002 |
* collections of tasks when some have been cancelled or otherwise |
|
1003 |
* known to have aborted. |
|
1004 |
*/ |
|
1005 |
public final void quietlyJoin() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1006 |
doJoin(); |
4110 | 1007 |
} |
1008 |
||
1009 |
/** |
|
1010 |
* Commences performing this task and awaits its completion if |
|
6543 | 1011 |
* necessary, without returning its result or throwing its |
1012 |
* exception. |
|
4110 | 1013 |
*/ |
1014 |
public final void quietlyInvoke() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1015 |
doInvoke(); |
4110 | 1016 |
} |
1017 |
||
1018 |
/** |
|
1019 |
* Possibly executes tasks until the pool hosting the current task |
|
1020 |
* {@link ForkJoinPool#isQuiescent is quiescent}. This method may |
|
1021 |
* be of use in designs in which many tasks are forked, but none |
|
1022 |
* are explicitly joined, instead executing them until all are |
|
1023 |
* processed. |
|
1024 |
* |
|
1025 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1026 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1027 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1028 |
* result in exceptions or errors, possibly including {@code |
|
1029 |
* ClassCastException}. |
|
1030 |
*/ |
|
1031 |
public static void helpQuiesce() { |
|
1032 |
((ForkJoinWorkerThread) Thread.currentThread()) |
|
1033 |
.helpQuiescePool(); |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
1037 |
* Resets the internal bookkeeping state of this task, allowing a |
|
1038 |
* subsequent {@code fork}. This method allows repeated reuse of |
|
1039 |
* this task, but only if reuse occurs when this task has either |
|
1040 |
* never been forked, or has been forked, then completed and all |
|
1041 |
* outstanding joins of this task have also completed. Effects |
|
1042 |
* under any other usage conditions are not guaranteed. |
|
1043 |
* This method may be useful when executing |
|
1044 |
* pre-constructed trees of subtasks in loops. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1045 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1046 |
* <p>Upon completion of this method, {@code isDone()} reports |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1047 |
* {@code false}, and {@code getException()} reports {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1048 |
* null}. However, the value returned by {@code getRawResult} is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1049 |
* unaffected. To clear this value, you can invoke {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1050 |
* setRawResult(null)}. |
4110 | 1051 |
*/ |
1052 |
public void reinitialize() { |
|
6543 | 1053 |
if (status == EXCEPTIONAL) |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1054 |
clearExceptionalCompletion(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1055 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1056 |
status = 0; |
4110 | 1057 |
} |
1058 |
||
1059 |
/** |
|
1060 |
* Returns the pool hosting the current task execution, or null |
|
1061 |
* if this task is executing outside of any ForkJoinPool. |
|
1062 |
* |
|
1063 |
* @see #inForkJoinPool |
|
1064 |
* @return the pool, or {@code null} if none |
|
1065 |
*/ |
|
1066 |
public static ForkJoinPool getPool() { |
|
1067 |
Thread t = Thread.currentThread(); |
|
1068 |
return (t instanceof ForkJoinWorkerThread) ? |
|
1069 |
((ForkJoinWorkerThread) t).pool : null; |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1073 |
* Returns {@code true} if the current thread is a {@link |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1074 |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation. |
4110 | 1075 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1076 |
* @return {@code true} if the current thread is a {@link |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1077 |
* ForkJoinWorkerThread} executing as a ForkJoinPool computation, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1078 |
* or {@code false} otherwise |
4110 | 1079 |
*/ |
1080 |
public static boolean inForkJoinPool() { |
|
1081 |
return Thread.currentThread() instanceof ForkJoinWorkerThread; |
|
1082 |
} |
|
1083 |
||
1084 |
/** |
|
1085 |
* Tries to unschedule this task for execution. This method will |
|
1086 |
* typically succeed if this task is the most recently forked task |
|
1087 |
* by the current thread, and has not commenced executing in |
|
1088 |
* another thread. This method may be useful when arranging |
|
1089 |
* alternative local processing of tasks that could have been, but |
|
1090 |
* were not, stolen. |
|
1091 |
* |
|
1092 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1093 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1094 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1095 |
* result in exceptions or errors, possibly including {@code |
|
1096 |
* ClassCastException}. |
|
1097 |
* |
|
1098 |
* @return {@code true} if unforked |
|
1099 |
*/ |
|
1100 |
public boolean tryUnfork() { |
|
1101 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1102 |
.unpushTask(this); |
|
1103 |
} |
|
1104 |
||
1105 |
/** |
|
1106 |
* Returns an estimate of the number of tasks that have been |
|
1107 |
* forked by the current worker thread but not yet executed. This |
|
1108 |
* value may be useful for heuristic decisions about whether to |
|
1109 |
* fork other tasks. |
|
1110 |
* |
|
1111 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1112 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1113 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1114 |
* result in exceptions or errors, possibly including {@code |
|
1115 |
* ClassCastException}. |
|
1116 |
* |
|
1117 |
* @return the number of tasks |
|
1118 |
*/ |
|
1119 |
public static int getQueuedTaskCount() { |
|
1120 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1121 |
.getQueueSize(); |
|
1122 |
} |
|
1123 |
||
1124 |
/** |
|
1125 |
* Returns an estimate of how many more locally queued tasks are |
|
1126 |
* held by the current worker thread than there are other worker |
|
1127 |
* threads that might steal them. This value may be useful for |
|
1128 |
* heuristic decisions about whether to fork other tasks. In many |
|
1129 |
* usages of ForkJoinTasks, at steady state, each worker should |
|
1130 |
* aim to maintain a small constant surplus (for example, 3) of |
|
1131 |
* tasks, and to process computations locally if this threshold is |
|
1132 |
* exceeded. |
|
1133 |
* |
|
1134 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1135 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1136 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1137 |
* result in exceptions or errors, possibly including {@code |
|
1138 |
* ClassCastException}. |
|
1139 |
* |
|
1140 |
* @return the surplus number of tasks, which may be negative |
|
1141 |
*/ |
|
1142 |
public static int getSurplusQueuedTaskCount() { |
|
1143 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1144 |
.getEstimatedSurplusTaskCount(); |
|
1145 |
} |
|
1146 |
||
1147 |
// Extension methods |
|
1148 |
||
1149 |
/** |
|
1150 |
* Returns the result that would be returned by {@link #join}, even |
|
1151 |
* if this task completed abnormally, or {@code null} if this task |
|
1152 |
* is not known to have been completed. This method is designed |
|
1153 |
* to aid debugging, as well as to support extensions. Its use in |
|
1154 |
* any other context is discouraged. |
|
1155 |
* |
|
1156 |
* @return the result, or {@code null} if not completed |
|
1157 |
*/ |
|
1158 |
public abstract V getRawResult(); |
|
1159 |
||
1160 |
/** |
|
1161 |
* Forces the given value to be returned as a result. This method |
|
1162 |
* is designed to support extensions, and should not in general be |
|
1163 |
* called otherwise. |
|
1164 |
* |
|
1165 |
* @param value the value |
|
1166 |
*/ |
|
1167 |
protected abstract void setRawResult(V value); |
|
1168 |
||
1169 |
/** |
|
1170 |
* Immediately performs the base action of this task. This method |
|
1171 |
* is designed to support extensions, and should not in general be |
|
1172 |
* called otherwise. The return value controls whether this task |
|
1173 |
* is considered to be done normally. It may return false in |
|
1174 |
* asynchronous actions that require explicit invocations of |
|
1175 |
* {@link #complete} to become joinable. It may also throw an |
|
1176 |
* (unchecked) exception to indicate abnormal exit. |
|
1177 |
* |
|
1178 |
* @return {@code true} if completed normally |
|
1179 |
*/ |
|
1180 |
protected abstract boolean exec(); |
|
1181 |
||
1182 |
/** |
|
1183 |
* Returns, but does not unschedule or execute, a task queued by |
|
1184 |
* the current thread but not yet executed, if one is immediately |
|
1185 |
* available. There is no guarantee that this task will actually |
|
1186 |
* be polled or executed next. Conversely, this method may return |
|
1187 |
* null even if a task exists but cannot be accessed without |
|
1188 |
* contention with other threads. This method is designed |
|
1189 |
* primarily to support extensions, and is unlikely to be useful |
|
1190 |
* otherwise. |
|
1191 |
* |
|
1192 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1193 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1194 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1195 |
* result in exceptions or errors, possibly including {@code |
|
1196 |
* ClassCastException}. |
|
1197 |
* |
|
1198 |
* @return the next task, or {@code null} if none are available |
|
1199 |
*/ |
|
1200 |
protected static ForkJoinTask<?> peekNextLocalTask() { |
|
1201 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1202 |
.peekTask(); |
|
1203 |
} |
|
1204 |
||
1205 |
/** |
|
1206 |
* Unschedules and returns, without executing, the next task |
|
1207 |
* queued by the current thread but not yet executed. This method |
|
1208 |
* is designed primarily to support extensions, and is unlikely to |
|
1209 |
* be useful otherwise. |
|
1210 |
* |
|
1211 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1212 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1213 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1214 |
* result in exceptions or errors, possibly including {@code |
|
1215 |
* ClassCastException}. |
|
1216 |
* |
|
1217 |
* @return the next task, or {@code null} if none are available |
|
1218 |
*/ |
|
1219 |
protected static ForkJoinTask<?> pollNextLocalTask() { |
|
1220 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1221 |
.pollLocalTask(); |
|
1222 |
} |
|
1223 |
||
1224 |
/** |
|
1225 |
* Unschedules and returns, without executing, the next task |
|
1226 |
* queued by the current thread but not yet executed, if one is |
|
1227 |
* available, or if not available, a task that was forked by some |
|
1228 |
* other thread, if available. Availability may be transient, so a |
|
1229 |
* {@code null} result does not necessarily imply quiescence |
|
1230 |
* of the pool this task is operating in. This method is designed |
|
1231 |
* primarily to support extensions, and is unlikely to be useful |
|
1232 |
* otherwise. |
|
1233 |
* |
|
1234 |
* <p>This method may be invoked only from within {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1235 |
* ForkJoinPool} computations (as may be determined using method |
4110 | 1236 |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1237 |
* result in exceptions or errors, possibly including {@code |
|
1238 |
* ClassCastException}. |
|
1239 |
* |
|
1240 |
* @return a task, or {@code null} if none are available |
|
1241 |
*/ |
|
1242 |
protected static ForkJoinTask<?> pollTask() { |
|
1243 |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
|
1244 |
.pollTask(); |
|
1245 |
} |
|
1246 |
||
1247 |
/** |
|
1248 |
* Adaptor for Runnables. This implements RunnableFuture |
|
1249 |
* to be compliant with AbstractExecutorService constraints |
|
1250 |
* when used in ForkJoinPool. |
|
1251 |
*/ |
|
1252 |
static final class AdaptedRunnable<T> extends ForkJoinTask<T> |
|
1253 |
implements RunnableFuture<T> { |
|
1254 |
final Runnable runnable; |
|
1255 |
final T resultOnCompletion; |
|
1256 |
T result; |
|
1257 |
AdaptedRunnable(Runnable runnable, T result) { |
|
1258 |
if (runnable == null) throw new NullPointerException(); |
|
1259 |
this.runnable = runnable; |
|
1260 |
this.resultOnCompletion = result; |
|
1261 |
} |
|
1262 |
public T getRawResult() { return result; } |
|
1263 |
public void setRawResult(T v) { result = v; } |
|
1264 |
public boolean exec() { |
|
1265 |
runnable.run(); |
|
1266 |
result = resultOnCompletion; |
|
1267 |
return true; |
|
1268 |
} |
|
1269 |
public void run() { invoke(); } |
|
1270 |
private static final long serialVersionUID = 5232453952276885070L; |
|
1271 |
} |
|
1272 |
||
1273 |
/** |
|
1274 |
* Adaptor for Callables |
|
1275 |
*/ |
|
1276 |
static final class AdaptedCallable<T> extends ForkJoinTask<T> |
|
1277 |
implements RunnableFuture<T> { |
|
1278 |
final Callable<? extends T> callable; |
|
1279 |
T result; |
|
1280 |
AdaptedCallable(Callable<? extends T> callable) { |
|
1281 |
if (callable == null) throw new NullPointerException(); |
|
1282 |
this.callable = callable; |
|
1283 |
} |
|
1284 |
public T getRawResult() { return result; } |
|
1285 |
public void setRawResult(T v) { result = v; } |
|
1286 |
public boolean exec() { |
|
1287 |
try { |
|
1288 |
result = callable.call(); |
|
1289 |
return true; |
|
1290 |
} catch (Error err) { |
|
1291 |
throw err; |
|
1292 |
} catch (RuntimeException rex) { |
|
1293 |
throw rex; |
|
1294 |
} catch (Exception ex) { |
|
1295 |
throw new RuntimeException(ex); |
|
1296 |
} |
|
1297 |
} |
|
1298 |
public void run() { invoke(); } |
|
1299 |
private static final long serialVersionUID = 2838392045355241008L; |
|
1300 |
} |
|
1301 |
||
1302 |
/** |
|
1303 |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
|
1304 |
* method of the given {@code Runnable} as its action, and returns |
|
1305 |
* a null result upon {@link #join}. |
|
1306 |
* |
|
1307 |
* @param runnable the runnable action |
|
1308 |
* @return the task |
|
1309 |
*/ |
|
1310 |
public static ForkJoinTask<?> adapt(Runnable runnable) { |
|
1311 |
return new AdaptedRunnable<Void>(runnable, null); |
|
1312 |
} |
|
1313 |
||
1314 |
/** |
|
1315 |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
|
1316 |
* method of the given {@code Runnable} as its action, and returns |
|
1317 |
* the given result upon {@link #join}. |
|
1318 |
* |
|
1319 |
* @param runnable the runnable action |
|
1320 |
* @param result the result upon completion |
|
1321 |
* @return the task |
|
1322 |
*/ |
|
1323 |
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
|
1324 |
return new AdaptedRunnable<T>(runnable, result); |
|
1325 |
} |
|
1326 |
||
1327 |
/** |
|
1328 |
* Returns a new {@code ForkJoinTask} that performs the {@code call} |
|
1329 |
* method of the given {@code Callable} as its action, and returns |
|
1330 |
* its result upon {@link #join}, translating any checked exceptions |
|
1331 |
* encountered into {@code RuntimeException}. |
|
1332 |
* |
|
1333 |
* @param callable the callable action |
|
1334 |
* @return the task |
|
1335 |
*/ |
|
1336 |
public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) { |
|
1337 |
return new AdaptedCallable<T>(callable); |
|
1338 |
} |
|
1339 |
||
1340 |
// Serialization support |
|
1341 |
||
1342 |
private static final long serialVersionUID = -7721805057305804111L; |
|
1343 |
||
1344 |
/** |
|
6543 | 1345 |
* Saves the state to a stream (that is, serializes it). |
4110 | 1346 |
* |
1347 |
* @serialData the current run status and the exception thrown |
|
1348 |
* during execution, or {@code null} if none |
|
1349 |
* @param s the stream |
|
1350 |
*/ |
|
1351 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1352 |
throws java.io.IOException { |
|
1353 |
s.defaultWriteObject(); |
|
1354 |
s.writeObject(getException()); |
|
1355 |
} |
|
1356 |
||
1357 |
/** |
|
6543 | 1358 |
* Reconstitutes the instance from a stream (that is, deserializes it). |
4110 | 1359 |
* |
1360 |
* @param s the stream |
|
1361 |
*/ |
|
1362 |
private void readObject(java.io.ObjectInputStream s) |
|
1363 |
throws java.io.IOException, ClassNotFoundException { |
|
1364 |
s.defaultReadObject(); |
|
1365 |
Object ex = s.readObject(); |
|
1366 |
if (ex != null) |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1367 |
setExceptionalCompletion((Throwable)ex); |
4110 | 1368 |
} |
1369 |
||
1370 |
// Unsafe mechanics |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1371 |
private static final sun.misc.Unsafe UNSAFE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1372 |
private static final long statusOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1373 |
static { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1374 |
exceptionTableLock = new ReentrantLock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1375 |
exceptionTableRefQueue = new ReferenceQueue<Object>(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1376 |
exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY]; |
4110 | 1377 |
try { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1378 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1379 |
statusOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1380 |
(ForkJoinTask.class.getDeclaredField("status")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1381 |
} catch (Exception e) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1382 |
throw new Error(e); |
4110 | 1383 |
} |
1384 |
} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1385 |
|
4110 | 1386 |
} |