ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
Revision: 1.69
Committed: Mon Feb 20 18:20:06 2012 UTC (12 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.68: +3 -3 lines
Log Message:
less conservative compensation

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.1 * Creates a ForkJoinWorkerThread operating in the given pool.
35 jsr166 1.11 *
36 dl 1.1 * @param pool the pool this thread works in
37     * @throws NullPointerException if pool is null
38     */
39     protected ForkJoinWorkerThread(ForkJoinPool pool) {
40 dl 1.63 super(pool.nextWorkerName());
41 dl 1.68 setDaemon(true);
42 dl 1.63 Thread.UncaughtExceptionHandler ueh = pool.ueh;
43 dl 1.31 if (ueh != null)
44     setUncaughtExceptionHandler(ueh);
45 dl 1.68 this.pool = pool;
46 dl 1.69 pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue
47     (pool, this, pool.localMode));
48 dl 1.31 }
49    
50 dl 1.2 /**
51 jsr166 1.11 * Returns the pool hosting this thread.
52     *
53 dl 1.2 * @return the pool
54     */
55 dl 1.4 public ForkJoinPool getPool() {
56     return pool;
57 dl 1.2 }
58    
59     /**
60 dl 1.4 * 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 dl 1.5 * collect results per-worker rather than per-task.
65 jsr166 1.11 *
66     * @return the index number
67 dl 1.2 */
68 dl 1.4 public int getPoolIndex() {
69 dl 1.68 return workQueue.poolIndex;
70 dl 1.2 }
71    
72 dl 1.7 /**
73 dl 1.31 * Initializes internal state after construction but before
74     * processing any tasks. If you override this method, you must
75 jsr166 1.58 * invoke {@code super.onStart()} at the beginning of the method.
76 dl 1.31 * 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 dl 1.7 */
81 dl 1.31 protected void onStart() {
82     }
83 dl 1.5
84     /**
85 dl 1.31 * 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.21 *
89 dl 1.31 * @param exception the exception causing this thread to abort due
90     * to an unrecoverable error, or {@code null} if completed normally
91 dl 1.5 */
92 dl 1.31 protected void onTermination(Throwable exception) {
93 dl 1.5 }
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 dl 1.59 * {@link ForkJoinTask}s.
99 dl 1.1 */
100 dl 1.5 public void run() {
101     Throwable exception = null;
102     try {
103     onStart();
104 dl 1.69 pool.runWorker(workQueue);
105 dl 1.5 } catch (Throwable ex) {
106     exception = ex;
107     } finally {
108 jsr166 1.6 try {
109 dl 1.68 onTermination(exception);
110     } catch (Throwable ex) {
111     if (exception == null)
112     exception = ex;
113     } finally {
114     pool.deregisterWorker(this, exception);
115 jsr166 1.6 }
116     }
117     }
118 dl 1.1 }
119 dl 1.68