ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
Revision: 1.73
Committed: Wed Nov 21 19:54:39 2012 UTC (11 years, 5 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.72: +10 -27 lines
Log Message:
stabilize

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.72 *
29     * This class just maintains links to its pool and WorkQueue. The
30 dl 1.73 * 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 dl 1.40 */
36    
37 dl 1.68 final ForkJoinPool pool; // the pool this thread works in
38 dl 1.73 final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics
39 dl 1.71
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.73 // Use a placeholder until a useful name can be set in registerWorker
48     super("aForkJoinWorkerThread");
49 dl 1.72 this.pool = pool;
50 dl 1.73 this.workQueue = pool.registerWorker(this);
51 dl 1.31 }
52    
53 dl 1.2 /**
54 jsr166 1.11 * Returns the pool hosting this thread.
55     *
56 dl 1.2 * @return the pool
57     */
58 dl 1.4 public ForkJoinPool getPool() {
59     return pool;
60 dl 1.2 }
61    
62     /**
63 dl 1.4 * 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 dl 1.5 * collect results per-worker rather than per-task.
68 jsr166 1.11 *
69     * @return the index number
70 dl 1.2 */
71 dl 1.4 public int getPoolIndex() {
72 dl 1.73 return workQueue.poolIndex;
73 dl 1.2 }
74    
75 dl 1.7 /**
76 dl 1.31 * Initializes internal state after construction but before
77     * processing any tasks. If you override this method, you must
78 jsr166 1.58 * invoke {@code super.onStart()} at the beginning of the method.
79 dl 1.31 * Initialization requires care: Most fields must have legal
80     * default values, to ensure that attempted accesses from other
81     * threads work correctly even before this thread starts
82     * processing tasks.
83 dl 1.7 */
84 dl 1.31 protected void onStart() {
85     }
86 dl 1.5
87     /**
88 dl 1.31 * Performs cleanup associated with termination of this worker
89     * thread. If you override this method, you must invoke
90     * {@code super.onTermination} at the end of the overridden method.
91 jsr166 1.21 *
92 dl 1.31 * @param exception the exception causing this thread to abort due
93     * to an unrecoverable error, or {@code null} if completed normally
94 dl 1.5 */
95 dl 1.31 protected void onTermination(Throwable exception) {
96 dl 1.5 }
97    
98     /**
99     * This method is required to be public, but should never be
100     * called explicitly. It performs the main run loop to execute
101 dl 1.59 * {@link ForkJoinTask}s.
102 dl 1.1 */
103 dl 1.5 public void run() {
104     Throwable exception = null;
105     try {
106     onStart();
107 dl 1.69 pool.runWorker(workQueue);
108 dl 1.5 } catch (Throwable ex) {
109     exception = ex;
110     } finally {
111 jsr166 1.6 try {
112 dl 1.68 onTermination(exception);
113     } catch (Throwable ex) {
114     if (exception == null)
115     exception = ex;
116     } finally {
117     pool.deregisterWorker(this, exception);
118 jsr166 1.6 }
119     }
120     }
121 dl 1.1 }