ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
Revision: 1.71
Committed: Wed Nov 14 17:20:38 2012 UTC (11 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.70: +13 -2 lines
Log Message:
commonPool support

File Contents

# User Rev Content
1 dl 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.64 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package jsr166y;
8 jsr166 1.18
9 dl 1.1 /**
10 dl 1.59 * A thread managed by a {@link ForkJoinPool}, which executes
11     * {@link ForkJoinTask}s.
12     * This class is subclassable solely for the sake of adding
13     * functionality -- there are no overridable methods dealing with
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}.
19 jsr166 1.6 *
20 jsr166 1.13 * @since 1.7
21     * @author Doug Lea
22 dl 1.1 */
23     public class ForkJoinWorkerThread extends Thread {
24     /*
25 dl 1.31 * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
26 dl 1.68 * ForkJoinTasks. For explanation, see the internal documentation
27     * of class ForkJoinPool.
28 dl 1.40 */
29    
30 dl 1.68 final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics
31     final ForkJoinPool pool; // the pool this thread works in
32 dl 1.36
33     /**
34 dl 1.71 * 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";
39    
40     /**
41 dl 1.1 * Creates a ForkJoinWorkerThread operating in the given pool.
42 jsr166 1.11 *
43 dl 1.1 * @param pool the pool this thread works in
44     * @throws NullPointerException if pool is null
45     */
46     protected ForkJoinWorkerThread(ForkJoinPool pool) {
47 dl 1.71 super(provisionalName); // bootstrap name
48 dl 1.63 Thread.UncaughtExceptionHandler ueh = pool.ueh;
49 dl 1.31 if (ueh != null)
50     setUncaughtExceptionHandler(ueh);
51 dl 1.71 setDaemon(true);
52 dl 1.68 this.pool = pool;
53 dl 1.69 pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue
54     (pool, this, pool.localMode));
55 dl 1.31 }
56    
57 dl 1.2 /**
58 jsr166 1.11 * Returns the pool hosting this thread.
59     *
60 dl 1.2 * @return the pool
61     */
62 dl 1.4 public ForkJoinPool getPool() {
63     return pool;
64 dl 1.2 }
65    
66     /**
67 dl 1.4 * Returns the index number of this thread in its pool. The
68     * returned value ranges from zero to the maximum number of
69     * threads (minus one) that have ever been created in the pool.
70     * This method may be useful for applications that track status or
71 dl 1.5 * collect results per-worker rather than per-task.
72 jsr166 1.11 *
73     * @return the index number
74 dl 1.2 */
75 dl 1.4 public int getPoolIndex() {
76 dl 1.68 return workQueue.poolIndex;
77 dl 1.2 }
78    
79 dl 1.7 /**
80 dl 1.31 * Initializes internal state after construction but before
81     * processing any tasks. If you override this method, you must
82 jsr166 1.58 * invoke {@code super.onStart()} at the beginning of the method.
83 dl 1.31 * Initialization requires care: Most fields must have legal
84     * default values, to ensure that attempted accesses from other
85     * threads work correctly even before this thread starts
86     * processing tasks.
87 dl 1.7 */
88 dl 1.31 protected void onStart() {
89 dl 1.71 String pref; // replace bootstrap name
90     if (provisionalName.equals(getName()) &&
91     (pref = pool.workerNamePrefix) != null)
92     setName(pref.concat(Long.toString(getId())));
93 dl 1.31 }
94 dl 1.5
95     /**
96 dl 1.31 * Performs cleanup associated with termination of this worker
97     * thread. If you override this method, you must invoke
98     * {@code super.onTermination} at the end of the overridden method.
99 jsr166 1.21 *
100 dl 1.31 * @param exception the exception causing this thread to abort due
101     * to an unrecoverable error, or {@code null} if completed normally
102 dl 1.5 */
103 dl 1.31 protected void onTermination(Throwable exception) {
104 dl 1.5 }
105    
106     /**
107     * This method is required to be public, but should never be
108     * called explicitly. It performs the main run loop to execute
109 dl 1.59 * {@link ForkJoinTask}s.
110 dl 1.1 */
111 dl 1.5 public void run() {
112     Throwable exception = null;
113     try {
114     onStart();
115 dl 1.69 pool.runWorker(workQueue);
116 dl 1.5 } catch (Throwable ex) {
117     exception = ex;
118     } finally {
119 jsr166 1.6 try {
120 dl 1.68 onTermination(exception);
121     } catch (Throwable ex) {
122     if (exception == null)
123     exception = ex;
124     } finally {
125     pool.deregisterWorker(this, exception);
126 jsr166 1.6 }
127     }
128     }
129 dl 1.1 }