6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
– |
import java.util.concurrent.atomic.AtomicInteger; |
10 |
– |
|
9 |
|
/** |
10 |
|
* A thread managed by a {@link ForkJoinPool}, which executes |
11 |
|
* {@link ForkJoinTask}s. |
27 |
|
* of class ForkJoinPool. |
28 |
|
* |
29 |
|
* This class just maintains links to its pool and WorkQueue. The |
30 |
< |
* pool field is set upon construction, but the workQueue field is |
31 |
< |
* not set until the thread has started (unless forced early by a |
32 |
< |
* subclass constructor call to poolIndex()). This provides |
33 |
< |
* better memory placement (because this thread allocates queue |
34 |
< |
* and bookkeeping fields) but because the field is non-final, we |
37 |
< |
* require that it never be accessed except by the owning thread. |
30 |
> |
* pool field is set immediately upon construction, but the |
31 |
> |
* workQueue field is not set until a call to registerWorker |
32 |
> |
* completes. This leads to a visibility race, that is tolerated |
33 |
> |
* by requiring that the workQueue field is only accessed by the |
34 |
> |
* owning thread. |
35 |
|
*/ |
36 |
|
|
37 |
|
final ForkJoinPool pool; // the pool this thread works in |
38 |
< |
ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
42 |
< |
|
43 |
< |
/** |
44 |
< |
* Sequence number for creating worker Names |
45 |
< |
*/ |
46 |
< |
private static final AtomicInteger threadNumber = new AtomicInteger(); |
38 |
> |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
39 |
|
|
40 |
|
/** |
41 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
44 |
|
* @throws NullPointerException if pool is null |
45 |
|
*/ |
46 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
47 |
< |
super(pool.workerNamePrefix.concat(Integer.toString(threadNumber.incrementAndGet()))); |
48 |
< |
setDaemon(true); |
47 |
> |
// Use a placeholder until a useful name can be set in registerWorker |
48 |
> |
super("aForkJoinWorkerThread"); |
49 |
|
this.pool = pool; |
50 |
< |
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
59 |
< |
if (ueh != null) |
60 |
< |
setUncaughtExceptionHandler(ueh); |
50 |
> |
this.workQueue = pool.registerWorker(this); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
69 |
|
* @return the index number |
70 |
|
*/ |
71 |
|
public int getPoolIndex() { |
72 |
< |
// 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; |
72 |
> |
return workQueue.poolIndex; |
73 |
|
} |
74 |
|
|
75 |
|
/** |
103 |
|
public void run() { |
104 |
|
Throwable exception = null; |
105 |
|
try { |
122 |
– |
pool.registerWorker(this); |
106 |
|
onStart(); |
107 |
|
pool.runWorker(workQueue); |
108 |
|
} catch (Throwable ex) { |