6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
– |
import java.util.concurrent.atomic.AtomicInteger; |
9 |
|
|
10 |
|
/** |
11 |
|
* A thread managed by a {@link ForkJoinPool}, which executes |
15 |
|
* scheduling or execution. However, you can override initialization |
16 |
|
* and termination methods surrounding the main task processing loop. |
17 |
|
* If you do create such a subclass, you will also need to supply a |
18 |
< |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it |
19 |
< |
* in a {@code ForkJoinPool}. |
18 |
> |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
19 |
> |
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
20 |
|
* |
21 |
|
* @since 1.7 |
22 |
|
* @author Doug Lea |
28 |
|
* of class ForkJoinPool. |
29 |
|
* |
30 |
|
* This class just maintains links to its pool and WorkQueue. The |
31 |
< |
* pool field is set upon construction, but the workQueue field is |
32 |
< |
* not set until the thread has started (unless forced early by a |
33 |
< |
* subclass constructor call to poolIndex()). This provides |
34 |
< |
* better memory placement (because this thread allocates queue |
35 |
< |
* and bookkeeping fields) but because the field is non-final, we |
37 |
< |
* require that it never be accessed except by the owning thread. |
31 |
> |
* pool field is set immediately upon construction, but the |
32 |
> |
* workQueue field is not set until a call to registerWorker |
33 |
> |
* completes. This leads to a visibility race, that is tolerated |
34 |
> |
* by requiring that the workQueue field is only accessed by the |
35 |
> |
* owning thread. |
36 |
|
*/ |
37 |
|
|
38 |
|
final ForkJoinPool pool; // the pool this thread works in |
39 |
< |
ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
42 |
< |
|
43 |
< |
/** |
44 |
< |
* Sequence number for creating worker Names |
45 |
< |
*/ |
46 |
< |
private static final AtomicInteger threadNumber = new AtomicInteger(); |
39 |
> |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
40 |
|
|
41 |
|
/** |
42 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
45 |
|
* @throws NullPointerException if pool is null |
46 |
|
*/ |
47 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
48 |
< |
super(pool.workerNamePrefix.concat(Integer.toString(threadNumber.incrementAndGet()))); |
49 |
< |
setDaemon(true); |
48 |
> |
// Use a placeholder until a useful name can be set in registerWorker |
49 |
> |
super("aForkJoinWorkerThread"); |
50 |
|
this.pool = pool; |
51 |
< |
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
59 |
< |
if (ueh != null) |
60 |
< |
setUncaughtExceptionHandler(ueh); |
51 |
> |
this.workQueue = pool.registerWorker(this); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
61 |
|
} |
62 |
|
|
63 |
|
/** |
64 |
< |
* Returns the index number of this thread in its pool. The |
65 |
< |
* returned value ranges from zero to the maximum number of |
66 |
< |
* threads (minus one) that have ever been created in the pool. |
67 |
< |
* This method may be useful for applications that track status or |
68 |
< |
* collect results per-worker rather than per-task. |
64 |
> |
* Returns the unique index number of this thread in its pool. |
65 |
> |
* The returned value ranges from zero to the maximum number of |
66 |
> |
* threads (minus one) that may exist in the pool, and does not |
67 |
> |
* change during the lifetime of the thread. This method may be |
68 |
> |
* useful for applications that track status or collect results |
69 |
> |
* per-worker-thread rather than per-task. |
70 |
|
* |
71 |
|
* @return the index number |
72 |
|
*/ |
73 |
|
public int getPoolIndex() { |
74 |
< |
// 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; |
74 |
> |
return workQueue.poolIndex >>> 1; // ignore odd/even tag bit |
75 |
|
} |
76 |
|
|
77 |
|
/** |
105 |
|
public void run() { |
106 |
|
Throwable exception = null; |
107 |
|
try { |
122 |
– |
pool.registerWorker(this); |
108 |
|
onStart(); |
109 |
|
pool.runWorker(workQueue); |
110 |
|
} catch (Throwable ex) { |