14 |
|
* scheduling or execution. However, you can override initialization |
15 |
|
* and termination methods surrounding the main task processing loop. |
16 |
|
* If you do create such a subclass, you will also need to supply a |
17 |
< |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it |
18 |
< |
* in a {@code ForkJoinPool}. |
17 |
> |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
18 |
> |
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
19 |
|
* |
20 |
|
* @since 1.7 |
21 |
|
* @author Doug Lea |
25 |
|
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
26 |
|
* ForkJoinTasks. For explanation, see the internal documentation |
27 |
|
* of class ForkJoinPool. |
28 |
+ |
* |
29 |
+ |
* This class just maintains links to its pool and WorkQueue. The |
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 |
|
|
30 |
– |
final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
37 |
|
final ForkJoinPool pool; // the pool this thread works in |
38 |
< |
|
33 |
< |
/** |
34 |
< |
* 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. |
37 |
< |
*/ |
38 |
< |
static final String provisionalName = "aForkJoinWorkerThread"; |
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(provisionalName); // bootstrap name |
48 |
< |
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
49 |
< |
if (ueh != null) |
50 |
< |
setUncaughtExceptionHandler(ueh); |
51 |
< |
setDaemon(true); |
47 |
> |
// Use a placeholder until a useful name can be set in registerWorker |
48 |
> |
super("aForkJoinWorkerThread"); |
49 |
|
this.pool = pool; |
50 |
< |
pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue |
54 |
< |
(pool, this, pool.localMode)); |
50 |
> |
this.workQueue = pool.registerWorker(this); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* Returns the index number of this thread in its pool. The |
64 |
< |
* returned value ranges from zero to the maximum number of |
65 |
< |
* threads (minus one) that have ever been created in the pool. |
66 |
< |
* This method may be useful for applications that track status or |
67 |
< |
* collect results per-worker rather than per-task. |
63 |
> |
* Returns the unique index number of this thread in its pool. |
64 |
> |
* The returned value ranges from zero to the maximum number of |
65 |
> |
* threads (minus one) that may exist in the pool, and does not |
66 |
> |
* change during the lifetime of the thread. This method may be |
67 |
> |
* useful for applications that track status or collect results |
68 |
> |
* per-worker-thread rather than per-task. |
69 |
|
* |
70 |
|
* @return the index number |
71 |
|
*/ |
72 |
|
public int getPoolIndex() { |
73 |
< |
return workQueue.poolIndex; |
73 |
> |
return workQueue.poolIndex >>> 1; // ignore odd/even tag bit |
74 |
|
} |
75 |
|
|
76 |
|
/** |
83 |
|
* processing tasks. |
84 |
|
*/ |
85 |
|
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()))); |
86 |
|
} |
87 |
|
|
88 |
|
/** |