6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
10 |
+ |
|
11 |
|
/** |
12 |
|
* A thread managed by a {@link ForkJoinPool}, which executes |
13 |
|
* {@link ForkJoinTask}s. |
27 |
|
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
28 |
|
* ForkJoinTasks. For explanation, see the internal documentation |
29 |
|
* of class ForkJoinPool. |
30 |
+ |
* |
31 |
+ |
* This class just maintains links to its pool and WorkQueue. The |
32 |
+ |
* pool field is set upon construction, but the workQueue field is |
33 |
+ |
* not set until the thread has started (unless forced early by a |
34 |
+ |
* subclass constructor call to poolIndex()). This provides |
35 |
+ |
* better memory placement (because this thread allocates queue |
36 |
+ |
* and bookkeeping fields) but because the field is non-final, we |
37 |
+ |
* require that it never be accessed except by the owning thread. |
38 |
|
*/ |
39 |
|
|
30 |
– |
final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
40 |
|
final ForkJoinPool pool; // the pool this thread works in |
41 |
+ |
ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
42 |
|
|
43 |
|
/** |
44 |
< |
* An initial name for a newly constructed worker, used until |
35 |
< |
* onStart can establish a useful name. This removes need to |
36 |
< |
* establish a name from worker startup path. |
44 |
> |
* Sequence number for creating worker Names |
45 |
|
*/ |
46 |
< |
static final String provisionalName = "aForkJoinWorkerThread"; |
46 |
> |
private static final AtomicInteger threadNumber = new AtomicInteger(); |
47 |
|
|
48 |
|
/** |
49 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
52 |
|
* @throws NullPointerException if pool is null |
53 |
|
*/ |
54 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
55 |
< |
super(provisionalName); // bootstrap name |
55 |
> |
super(pool.workerNamePrefix.concat(Integer.toString(threadNumber.incrementAndGet()))); |
56 |
> |
setDaemon(true); |
57 |
> |
this.pool = pool; |
58 |
|
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
59 |
|
if (ueh != null) |
60 |
|
setUncaughtExceptionHandler(ueh); |
51 |
– |
setDaemon(true); |
52 |
– |
this.pool = pool; |
53 |
– |
pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue |
54 |
– |
(pool, this, pool.localMode)); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
79 |
|
* @return the index number |
80 |
|
*/ |
81 |
|
public int getPoolIndex() { |
82 |
< |
return workQueue.poolIndex; |
82 |
> |
// force early registration if called before started |
83 |
> |
ForkJoinPool.WorkQueue q; |
84 |
> |
if ((q = workQueue) == null) { |
85 |
> |
pool.registerWorker(this); |
86 |
> |
q = workQueue; |
87 |
> |
} |
88 |
> |
return q.poolIndex; |
89 |
|
} |
90 |
|
|
91 |
|
/** |
98 |
|
* processing tasks. |
99 |
|
*/ |
100 |
|
protected void onStart() { |
89 |
– |
String pref; // replace bootstrap name |
90 |
– |
if (provisionalName.equals(getName()) && |
91 |
– |
(pref = pool.workerNamePrefix) != null) |
92 |
– |
setName(pref.concat(Long.toString(getId()))); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
119 |
|
public void run() { |
120 |
|
Throwable exception = null; |
121 |
|
try { |
122 |
+ |
pool.registerWorker(this); |
123 |
|
onStart(); |
124 |
|
pool.runWorker(workQueue); |
125 |
|
} catch (Throwable ex) { |