1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.security.AccessControlContext; |
10 |
import java.security.AccessController; |
11 |
import java.security.PrivilegedAction; |
12 |
import java.security.ProtectionDomain; |
13 |
|
14 |
/** |
15 |
* A thread managed by a {@link ForkJoinPool}, which executes |
16 |
* {@link ForkJoinTask}s. |
17 |
* This class is subclassable solely for the sake of adding |
18 |
* functionality -- there are no overridable methods dealing with |
19 |
* scheduling or execution. However, you can override initialization |
20 |
* and termination methods surrounding the main task processing loop. |
21 |
* If you do create such a subclass, you will also need to supply a |
22 |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
23 |
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
24 |
* |
25 |
* @since 1.7 |
26 |
* @author Doug Lea |
27 |
*/ |
28 |
public class ForkJoinWorkerThread extends Thread { |
29 |
/* |
30 |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
31 |
* ForkJoinTasks. For explanation, see the internal documentation |
32 |
* of class ForkJoinPool. |
33 |
* |
34 |
* This class just maintains links to its pool and WorkQueue. The |
35 |
* pool field is set immediately upon construction, but the |
36 |
* workQueue field is not set until a call to registerWorker |
37 |
* completes. This leads to a visibility race, that is tolerated |
38 |
* by requiring that the workQueue field is only accessed by the |
39 |
* owning thread. |
40 |
* |
41 |
* Support for (non-public) subclass InnocuousForkJoinWorkerThread |
42 |
* requires that we break quite a lot of encapsulation (via helper |
43 |
* methods in ThreadLocalRandom) both here and in the subclass to |
44 |
* access and set Thread fields. |
45 |
*/ |
46 |
|
47 |
final ForkJoinPool pool; // the pool this thread works in |
48 |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
49 |
|
50 |
/** |
51 |
* Creates a ForkJoinWorkerThread operating in the given pool. |
52 |
* |
53 |
* @param pool the pool this thread works in |
54 |
* @throws NullPointerException if pool is null |
55 |
*/ |
56 |
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
57 |
// Use a placeholder until a useful name can be set in registerWorker |
58 |
super("aForkJoinWorkerThread"); |
59 |
this.pool = pool; |
60 |
this.workQueue = pool.registerWorker(this); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Version for use by the default pool. Supports setting the |
65 |
* context class loader. This is a separate constructor to avoid |
66 |
* affecting the protected constructor. |
67 |
*/ |
68 |
ForkJoinWorkerThread(ForkJoinPool pool, ClassLoader ccl) { |
69 |
super("aForkJoinWorkerThread"); |
70 |
super.setContextClassLoader(ccl); |
71 |
this.pool = pool; |
72 |
this.workQueue = pool.registerWorker(this); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Version for InnocuousForkJoinWorkerThread. |
77 |
*/ |
78 |
ForkJoinWorkerThread(ForkJoinPool pool, |
79 |
ClassLoader ccl, |
80 |
ThreadGroup threadGroup, |
81 |
AccessControlContext acc) { |
82 |
super(threadGroup, null, "aForkJoinWorkerThread"); |
83 |
super.setContextClassLoader(ccl); |
84 |
ThreadLocalRandom.setInheritedAccessControlContext(this, acc); |
85 |
ThreadLocalRandom.eraseThreadLocals(this); // clear before registering |
86 |
this.pool = pool; |
87 |
this.workQueue = pool.registerWorker(this); |
88 |
} |
89 |
|
90 |
/** |
91 |
* Returns the pool hosting this thread. |
92 |
* |
93 |
* @return the pool |
94 |
*/ |
95 |
public ForkJoinPool getPool() { |
96 |
return pool; |
97 |
} |
98 |
|
99 |
/** |
100 |
* Returns the unique index number of this thread in its pool. |
101 |
* The returned value ranges from zero to the maximum number of |
102 |
* threads (minus one) that may exist in the pool, and does not |
103 |
* change during the lifetime of the thread. This method may be |
104 |
* useful for applications that track status or collect results |
105 |
* per-worker-thread rather than per-task. |
106 |
* |
107 |
* @return the index number |
108 |
*/ |
109 |
public int getPoolIndex() { |
110 |
return workQueue.getPoolIndex(); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Initializes internal state after construction but before |
115 |
* processing any tasks. If you override this method, you must |
116 |
* invoke {@code super.onStart()} at the beginning of the method. |
117 |
* Initialization requires care: Most fields must have legal |
118 |
* default values, to ensure that attempted accesses from other |
119 |
* threads work correctly even before this thread starts |
120 |
* processing tasks. |
121 |
*/ |
122 |
protected void onStart() { |
123 |
} |
124 |
|
125 |
/** |
126 |
* Performs cleanup associated with termination of this worker |
127 |
* thread. If you override this method, you must invoke |
128 |
* {@code super.onTermination} at the end of the overridden method. |
129 |
* |
130 |
* @param exception the exception causing this thread to abort due |
131 |
* to an unrecoverable error, or {@code null} if completed normally |
132 |
*/ |
133 |
protected void onTermination(Throwable exception) { |
134 |
} |
135 |
|
136 |
/** |
137 |
* This method is required to be public, but should never be |
138 |
* called explicitly. It performs the main run loop to execute |
139 |
* {@link ForkJoinTask}s. |
140 |
*/ |
141 |
public void run() { |
142 |
if (workQueue.array == null) { // only run once |
143 |
Throwable exception = null; |
144 |
try { |
145 |
onStart(); |
146 |
pool.runWorker(workQueue); |
147 |
} catch (Throwable ex) { |
148 |
exception = ex; |
149 |
} finally { |
150 |
try { |
151 |
onTermination(exception); |
152 |
} catch (Throwable ex) { |
153 |
if (exception == null) |
154 |
exception = ex; |
155 |
} finally { |
156 |
pool.deregisterWorker(this, exception); |
157 |
} |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
/** |
163 |
* Non-public hook method for InnocuousForkJoinWorkerThread. |
164 |
*/ |
165 |
void afterTopLevelExec() { |
166 |
} |
167 |
|
168 |
/** |
169 |
* A worker thread that has no permissions, is not a member of any |
170 |
* user-defined ThreadGroup, uses the system class loader as |
171 |
* thread context class loader, and erases all ThreadLocals after |
172 |
* running each top-level task. |
173 |
*/ |
174 |
static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread { |
175 |
/** The ThreadGroup for all InnocuousForkJoinWorkerThreads */ |
176 |
private static final ThreadGroup innocuousThreadGroup = |
177 |
java.security.AccessController.doPrivileged( |
178 |
new java.security.PrivilegedAction<>() { |
179 |
public ThreadGroup run() { |
180 |
ThreadGroup group = Thread.currentThread().getThreadGroup(); |
181 |
for (ThreadGroup p; (p = group.getParent()) != null; ) |
182 |
group = p; |
183 |
return new ThreadGroup(group, "InnocuousForkJoinWorkerThreadGroup"); |
184 |
}}); |
185 |
|
186 |
/** An AccessControlContext supporting no privileges */ |
187 |
private static final AccessControlContext INNOCUOUS_ACC = |
188 |
new AccessControlContext( |
189 |
new ProtectionDomain[] { |
190 |
new ProtectionDomain(null, null) |
191 |
}); |
192 |
|
193 |
InnocuousForkJoinWorkerThread(ForkJoinPool pool) { |
194 |
super(pool, |
195 |
ClassLoader.getSystemClassLoader(), |
196 |
innocuousThreadGroup, |
197 |
INNOCUOUS_ACC); |
198 |
} |
199 |
|
200 |
@Override // to erase ThreadLocals |
201 |
void afterTopLevelExec() { |
202 |
ThreadLocalRandom.eraseThreadLocals(this); |
203 |
} |
204 |
|
205 |
@Override // to silently fail |
206 |
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { } |
207 |
|
208 |
@Override // paranoically |
209 |
public void setContextClassLoader(ClassLoader cl) { |
210 |
throw new SecurityException("setContextClassLoader"); |
211 |
} |
212 |
} |
213 |
} |