ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ForkJoinWorkerThread.java
Revision: 1.4
Committed: Wed Nov 21 19:54:32 2012 UTC (11 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.3: +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     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6    
7     package jsr166e;
8    
9     /**
10     * 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     *
20     * @since 1.7
21     * @author Doug Lea
22     */
23     public class ForkJoinWorkerThread extends Thread {
24     /*
25     * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
26     * ForkJoinTasks. For explanation, see the internal documentation
27     * of class ForkJoinPool.
28 dl 1.3 *
29     * This class just maintains links to its pool and WorkQueue. The
30 dl 1.4 * 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.1 */
36    
37     final ForkJoinPool pool; // the pool this thread works in
38 dl 1.4 final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics
39 dl 1.2
40     /**
41 dl 1.1 * Creates a ForkJoinWorkerThread operating in the given pool.
42     *
43     * @param pool the pool this thread works in
44     * @throws NullPointerException if pool is null
45     */
46     protected ForkJoinWorkerThread(ForkJoinPool pool) {
47 dl 1.4 // Use a placeholder until a useful name can be set in registerWorker
48     super("aForkJoinWorkerThread");
49 dl 1.3 this.pool = pool;
50 dl 1.4 this.workQueue = pool.registerWorker(this);
51 dl 1.1 }
52    
53     /**
54     * Returns the pool hosting this thread.
55     *
56     * @return the pool
57     */
58     public ForkJoinPool getPool() {
59     return pool;
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.
68     *
69     * @return the index number
70     */
71     public int getPoolIndex() {
72 dl 1.4 return workQueue.poolIndex;
73 dl 1.1 }
74    
75     /**
76     * Initializes internal state after construction but before
77     * processing any tasks. If you override this method, you must
78     * invoke {@code super.onStart()} at the beginning of the method.
79     * 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     */
84     protected void onStart() {
85     }
86    
87     /**
88     * 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     *
92     * @param exception the exception causing this thread to abort due
93     * to an unrecoverable error, or {@code null} if completed normally
94     */
95     protected void onTermination(Throwable exception) {
96     }
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     * {@link ForkJoinTask}s.
102     */
103     public void run() {
104     Throwable exception = null;
105     try {
106     onStart();
107     pool.runWorker(workQueue);
108     } catch (Throwable ex) {
109     exception = ex;
110     } finally {
111     try {
112     onTermination(exception);
113     } catch (Throwable ex) {
114     if (exception == null)
115     exception = ex;
116     } finally {
117     pool.deregisterWorker(this, exception);
118     }
119     }
120     }
121     }