ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ForkJoinWorkerThread.java
Revision: 1.52
Committed: Thu Jan 26 00:08:17 2012 UTC (12 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.51: +17 -868 lines
Log Message:
Preliminary release of next version

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     /**
10 jsr166 1.40 * 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.1 *
20     * @since 1.7
21     * @author Doug Lea
22     */
23     public class ForkJoinWorkerThread extends Thread {
24     /*
25 dl 1.14 * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
26 dl 1.52 * ForkJoinTasks. For explanation, see the internal documentation
27     * of class ForkJoinPool.
28 jsr166 1.1 */
29    
30 dl 1.52 final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics
31     final ForkJoinPool pool; // the pool this thread works in
32 dl 1.18
33     /**
34 jsr166 1.1 * Creates a ForkJoinWorkerThread operating in the given pool.
35     *
36     * @param pool the pool this thread works in
37     * @throws NullPointerException if pool is null
38     */
39     protected ForkJoinWorkerThread(ForkJoinPool pool) {
40 dl 1.45 super(pool.nextWorkerName());
41 dl 1.52 setDaemon(true);
42 dl 1.45 Thread.UncaughtExceptionHandler ueh = pool.ueh;
43 dl 1.14 if (ueh != null)
44     setUncaughtExceptionHandler(ueh);
45 dl 1.52 this.pool = pool;
46     this.workQueue = new ForkJoinPool.WorkQueue(this, pool.localMode);
47     pool.registerWorker(this);
48 dl 1.14 }
49    
50 jsr166 1.1 /**
51     * Returns the pool hosting this thread.
52     *
53     * @return the pool
54     */
55     public ForkJoinPool getPool() {
56     return pool;
57     }
58    
59     /**
60     * Returns the index number of this thread in its pool. The
61     * returned value ranges from zero to the maximum number of
62     * threads (minus one) that have ever been created in the pool.
63     * This method may be useful for applications that track status or
64     * collect results per-worker rather than per-task.
65     *
66     * @return the index number
67     */
68     public int getPoolIndex() {
69 dl 1.52 return workQueue.poolIndex;
70 dl 1.45 }
71    
72 jsr166 1.1 /**
73 dl 1.14 * Initializes internal state after construction but before
74     * processing any tasks. If you override this method, you must
75 jsr166 1.39 * invoke {@code super.onStart()} at the beginning of the method.
76 dl 1.14 * Initialization requires care: Most fields must have legal
77     * default values, to ensure that attempted accesses from other
78     * threads work correctly even before this thread starts
79     * processing tasks.
80 jsr166 1.1 */
81 dl 1.14 protected void onStart() {
82     }
83 jsr166 1.1
84     /**
85 dl 1.14 * Performs cleanup associated with termination of this worker
86     * thread. If you override this method, you must invoke
87     * {@code super.onTermination} at the end of the overridden method.
88 jsr166 1.4 *
89 dl 1.14 * @param exception the exception causing this thread to abort due
90     * to an unrecoverable error, or {@code null} if completed normally
91 jsr166 1.1 */
92 dl 1.14 protected void onTermination(Throwable exception) {
93 jsr166 1.1 }
94    
95     /**
96     * This method is required to be public, but should never be
97     * called explicitly. It performs the main run loop to execute
98 jsr166 1.40 * {@link ForkJoinTask}s.
99 jsr166 1.1 */
100     public void run() {
101     Throwable exception = null;
102     try {
103     onStart();
104 dl 1.52 pool.runWorker(this);
105 jsr166 1.1 } catch (Throwable ex) {
106     exception = ex;
107     } finally {
108 dl 1.52 try {
109     onTermination(exception);
110     } catch (Throwable ex) {
111     if (exception == null)
112     exception = ex;
113     } finally {
114     pool.deregisterWorker(this, exception);
115 dl 1.45 }
116     }
117 jsr166 1.1 }
118     }