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