ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ForkJoinWorkerThread.java
Revision: 1.55
Committed: Mon Nov 19 18:12:34 2012 UTC (11 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.54: +24 -15 lines
Log Message:
better memory positioning

File Contents

# User Rev Content
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.55 import java.util.concurrent.atomic.AtomicInteger;
10    
11 jsr166 1.1 /**
12 jsr166 1.40 * A thread managed by a {@link ForkJoinPool}, which executes
13     * {@link ForkJoinTask}s.
14     * This class is subclassable solely for the sake of adding
15     * functionality -- there are no overridable methods dealing with
16     * scheduling or execution. However, you can override initialization
17     * and termination methods surrounding the main task processing loop.
18     * If you do create such a subclass, you will also need to supply a
19     * custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it
20     * in a {@code ForkJoinPool}.
21 jsr166 1.1 *
22     * @since 1.7
23     * @author Doug Lea
24     */
25     public class ForkJoinWorkerThread extends Thread {
26     /*
27 dl 1.14 * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
28 dl 1.52 * ForkJoinTasks. For explanation, see the internal documentation
29     * of class ForkJoinPool.
30 dl 1.55 *
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 jsr166 1.1 */
39    
40 dl 1.52 final ForkJoinPool pool; // the pool this thread works in
41 dl 1.55 ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics
42 dl 1.18
43     /**
44 dl 1.55 * Sequence number for creating worker Names
45 dl 1.54 */
46 dl 1.55 private static final AtomicInteger threadNumber = new AtomicInteger();
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.55 super(pool.workerNamePrefix.concat(Integer.toString(threadNumber.incrementAndGet())));
56     setDaemon(true);
57     this.pool = pool;
58 dl 1.45 Thread.UncaughtExceptionHandler ueh = pool.ueh;
59 dl 1.14 if (ueh != null)
60     setUncaughtExceptionHandler(ueh);
61     }
62    
63 jsr166 1.1 /**
64     * Returns the pool hosting this thread.
65     *
66     * @return the pool
67     */
68     public ForkJoinPool getPool() {
69     return pool;
70     }
71    
72     /**
73     * Returns the index number of this thread in its pool. The
74     * returned value ranges from zero to the maximum number of
75     * threads (minus one) that have ever been created in the pool.
76     * This method may be useful for applications that track status or
77     * collect results per-worker rather than per-task.
78     *
79     * @return the index number
80     */
81     public int getPoolIndex() {
82 dl 1.55 // 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 dl 1.45 }
90    
91 jsr166 1.1 /**
92 dl 1.14 * Initializes internal state after construction but before
93     * processing any tasks. If you override this method, you must
94 jsr166 1.39 * invoke {@code super.onStart()} at the beginning of the method.
95 dl 1.14 * Initialization requires care: Most fields must have legal
96     * default values, to ensure that attempted accesses from other
97     * threads work correctly even before this thread starts
98     * processing tasks.
99 jsr166 1.1 */
100 dl 1.14 protected void onStart() {
101     }
102 jsr166 1.1
103     /**
104 dl 1.14 * Performs cleanup associated with termination of this worker
105     * thread. If you override this method, you must invoke
106     * {@code super.onTermination} at the end of the overridden method.
107 jsr166 1.4 *
108 dl 1.14 * @param exception the exception causing this thread to abort due
109     * to an unrecoverable error, or {@code null} if completed normally
110 jsr166 1.1 */
111 dl 1.14 protected void onTermination(Throwable exception) {
112 jsr166 1.1 }
113    
114     /**
115     * This method is required to be public, but should never be
116     * called explicitly. It performs the main run loop to execute
117 jsr166 1.40 * {@link ForkJoinTask}s.
118 jsr166 1.1 */
119     public void run() {
120     Throwable exception = null;
121     try {
122 dl 1.55 pool.registerWorker(this);
123 jsr166 1.1 onStart();
124 dl 1.53 pool.runWorker(workQueue);
125 jsr166 1.1 } catch (Throwable ex) {
126     exception = ex;
127     } finally {
128 dl 1.52 try {
129     onTermination(exception);
130     } catch (Throwable ex) {
131     if (exception == null)
132     exception = ex;
133     } finally {
134     pool.deregisterWorker(this, exception);
135 dl 1.45 }
136     }
137 jsr166 1.1 }
138     }