author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 39723 | 9aa34e4a0469 |
child 43219 | 566b551b7820 |
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 |
||
22070 | 38 |
import java.security.AccessControlContext; |
39 |
import java.security.ProtectionDomain; |
|
40 |
||
4110 | 41 |
/** |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
42 |
* A thread managed by a {@link ForkJoinPool}, which executes |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
43 |
* {@link ForkJoinTask}s. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
44 |
* This class is subclassable solely for the sake of adding |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
45 |
* functionality -- there are no overridable methods dealing with |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
46 |
* scheduling or execution. However, you can override initialization |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
47 |
* and termination methods surrounding the main task processing loop. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
48 |
* If you do create such a subclass, you will also need to supply a |
18790 | 49 |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
50 |
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
|
4110 | 51 |
* |
52 |
* @since 1.7 |
|
53 |
* @author Doug Lea |
|
54 |
*/ |
|
55 |
public class ForkJoinWorkerThread extends Thread { |
|
56 |
/* |
|
6543 | 57 |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
58 |
* ForkJoinTasks. For explanation, see the internal documentation |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
59 |
* of class ForkJoinPool. |
4110 | 60 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
61 |
* This class just maintains links to its pool and WorkQueue. The |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
62 |
* pool field is set immediately upon construction, but the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
63 |
* workQueue field is not set until a call to registerWorker |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
64 |
* completes. This leads to a visibility race, that is tolerated |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
65 |
* by requiring that the workQueue field is only accessed by the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
66 |
* owning thread. |
22070 | 67 |
* |
68 |
* Support for (non-public) subclass InnocuousForkJoinWorkerThread |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
69 |
* requires that we break quite a lot of encapsulation (via helper |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
70 |
* methods in ThreadLocalRandom) both here and in the subclass to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
71 |
* access and set Thread fields. |
4110 | 72 |
*/ |
73 |
||
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
74 |
final ForkJoinPool pool; // the pool this thread works in |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
75 |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
4110 | 76 |
|
77 |
/** |
|
78 |
* Creates a ForkJoinWorkerThread operating in the given pool. |
|
79 |
* |
|
80 |
* @param pool the pool this thread works in |
|
81 |
* @throws NullPointerException if pool is null |
|
82 |
*/ |
|
83 |
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
84 |
// Use a placeholder until a useful name can be set in registerWorker |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
85 |
super("aForkJoinWorkerThread"); |
4110 | 86 |
this.pool = pool; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
87 |
this.workQueue = pool.registerWorker(this); |
4110 | 88 |
} |
89 |
||
90 |
/** |
|
32988 | 91 |
* Version for InnocuousForkJoinWorkerThread. |
22070 | 92 |
*/ |
93 |
ForkJoinWorkerThread(ForkJoinPool pool, ThreadGroup threadGroup, |
|
94 |
AccessControlContext acc) { |
|
95 |
super(threadGroup, null, "aForkJoinWorkerThread"); |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
96 |
ThreadLocalRandom.setInheritedAccessControlContext(this, acc); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
97 |
ThreadLocalRandom.eraseThreadLocals(this); // clear before registering |
22070 | 98 |
this.pool = pool; |
99 |
this.workQueue = pool.registerWorker(this); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
4110 | 103 |
* Returns the pool hosting this thread. |
104 |
* |
|
105 |
* @return the pool |
|
106 |
*/ |
|
107 |
public ForkJoinPool getPool() { |
|
108 |
return pool; |
|
109 |
} |
|
110 |
||
111 |
/** |
|
18790 | 112 |
* Returns the unique index number of this thread in its pool. |
113 |
* The returned value ranges from zero to the maximum number of |
|
114 |
* threads (minus one) that may exist in the pool, and does not |
|
115 |
* change during the lifetime of the thread. This method may be |
|
116 |
* useful for applications that track status or collect results |
|
117 |
* per-worker-thread rather than per-task. |
|
4110 | 118 |
* |
119 |
* @return the index number |
|
120 |
*/ |
|
121 |
public int getPoolIndex() { |
|
26448 | 122 |
return workQueue.getPoolIndex(); |
4110 | 123 |
} |
124 |
||
125 |
/** |
|
126 |
* Initializes internal state after construction but before |
|
127 |
* processing any tasks. If you override this method, you must |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
128 |
* invoke {@code super.onStart()} at the beginning of the method. |
4110 | 129 |
* Initialization requires care: Most fields must have legal |
130 |
* default values, to ensure that attempted accesses from other |
|
131 |
* threads work correctly even before this thread starts |
|
132 |
* processing tasks. |
|
133 |
*/ |
|
134 |
protected void onStart() { |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Performs cleanup associated with termination of this worker |
|
139 |
* thread. If you override this method, you must invoke |
|
140 |
* {@code super.onTermination} at the end of the overridden method. |
|
141 |
* |
|
142 |
* @param exception the exception causing this thread to abort due |
|
143 |
* to an unrecoverable error, or {@code null} if completed normally |
|
144 |
*/ |
|
145 |
protected void onTermination(Throwable exception) { |
|
6543 | 146 |
} |
147 |
||
148 |
/** |
|
149 |
* This method is required to be public, but should never be |
|
150 |
* called explicitly. It performs the main run loop to execute |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
151 |
* {@link ForkJoinTask}s. |
6543 | 152 |
*/ |
153 |
public void run() { |
|
22070 | 154 |
if (workQueue.array == null) { // only run once |
155 |
Throwable exception = null; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
156 |
try { |
22070 | 157 |
onStart(); |
158 |
pool.runWorker(workQueue); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
159 |
} catch (Throwable ex) { |
22070 | 160 |
exception = ex; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
161 |
} finally { |
22070 | 162 |
try { |
163 |
onTermination(exception); |
|
164 |
} catch (Throwable ex) { |
|
165 |
if (exception == null) |
|
166 |
exception = ex; |
|
167 |
} finally { |
|
168 |
pool.deregisterWorker(this, exception); |
|
169 |
} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
170 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
171 |
} |
4110 | 172 |
} |
22070 | 173 |
|
174 |
/** |
|
32988 | 175 |
* Non-public hook method for InnocuousForkJoinWorkerThread. |
22070 | 176 |
*/ |
177 |
void afterTopLevelExec() { |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* A worker thread that has no permissions, is not a member of any |
|
182 |
* user-defined ThreadGroup, and erases all ThreadLocals after |
|
183 |
* running each top-level task. |
|
184 |
*/ |
|
185 |
static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread { |
|
186 |
/** The ThreadGroup for all InnocuousForkJoinWorkerThreads */ |
|
187 |
private static final ThreadGroup innocuousThreadGroup = |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
188 |
ThreadLocalRandom.createThreadGroup("InnocuousForkJoinWorkerThreadGroup"); |
22070 | 189 |
|
190 |
/** An AccessControlContext supporting no privileges */ |
|
191 |
private static final AccessControlContext INNOCUOUS_ACC = |
|
192 |
new AccessControlContext( |
|
193 |
new ProtectionDomain[] { |
|
194 |
new ProtectionDomain(null, null) |
|
195 |
}); |
|
196 |
||
197 |
InnocuousForkJoinWorkerThread(ForkJoinPool pool) { |
|
198 |
super(pool, innocuousThreadGroup, INNOCUOUS_ACC); |
|
199 |
} |
|
200 |
||
201 |
@Override // to erase ThreadLocals |
|
202 |
void afterTopLevelExec() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
203 |
ThreadLocalRandom.eraseThreadLocals(this); |
22070 | 204 |
} |
205 |
||
206 |
@Override // to always report system loader |
|
207 |
public ClassLoader getContextClassLoader() { |
|
208 |
return ClassLoader.getSystemClassLoader(); |
|
209 |
} |
|
210 |
||
211 |
@Override // to silently fail |
|
212 |
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { } |
|
213 |
||
214 |
@Override // paranoically |
|
215 |
public void setContextClassLoader(ClassLoader cl) { |
|
216 |
throw new SecurityException("setContextClassLoader"); |
|
217 |
} |
|
218 |
||
219 |
} |
|
4110 | 220 |
} |