ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ForkJoinWorkerThread.java
(Generate patch)

Comparing jsr166/src/jsr166e/ForkJoinWorkerThread.java (file contents):
Revision 1.2 by dl, Wed Nov 14 17:20:29 2012 UTC vs.
Revision 1.3 by dl, Mon Nov 19 18:12:28 2012 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8  
9 + import java.util.concurrent.atomic.AtomicInteger;
10 +
11   /**
12   * A thread managed by a {@link ForkJoinPool}, which executes
13   * {@link ForkJoinTask}s.
# Line 25 | Line 27 | public class ForkJoinWorkerThread extend
27       * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
28       * ForkJoinTasks. For explanation, see the internal documentation
29       * of class ForkJoinPool.
30 +     *
31 +     * This class just maintains links to its pool and WorkQueue.  The
32 +     * pool field is set upon construction, but the workQueue field is
33 +     * not set until the thread has started (unless forced early by a
34 +     * subclass constructor call to poolIndex()).  This provides
35 +     * better memory placement (because this thread allocates queue
36 +     * and bookkeeping fields) but because the field is non-final, we
37 +     * require that it never be accessed except by the owning thread.
38       */
39  
30    final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics
40      final ForkJoinPool pool;                // the pool this thread works in
41 +    ForkJoinPool.WorkQueue workQueue;       // Work-stealing mechanics
42  
43      /**
44 <     * 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.
44 >     * Sequence number for creating worker Names
45       */
46 <    static final String provisionalName = "aForkJoinWorkerThread";
46 >    private static final AtomicInteger threadNumber = new AtomicInteger();
47  
48      /**
49       * Creates a ForkJoinWorkerThread operating in the given pool.
# Line 44 | Line 52 | public class ForkJoinWorkerThread extend
52       * @throws NullPointerException if pool is null
53       */
54      protected ForkJoinWorkerThread(ForkJoinPool pool) {
55 <        super(provisionalName); // bootstrap name
55 >        super(pool.workerNamePrefix.concat(Integer.toString(threadNumber.incrementAndGet())));
56 >        setDaemon(true);
57 >        this.pool = pool;
58          Thread.UncaughtExceptionHandler ueh = pool.ueh;
59          if (ueh != null)
60              setUncaughtExceptionHandler(ueh);
51        setDaemon(true);
52        this.pool = pool;
53        pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue
54                            (pool, this, pool.localMode));
61      }
62  
63      /**
# Line 73 | Line 79 | public class ForkJoinWorkerThread extend
79       * @return the index number
80       */
81      public int getPoolIndex() {
82 <        return workQueue.poolIndex;
82 >        // force early registration if called before started
83 >        ForkJoinPool.WorkQueue q;
84 >        if ((q = workQueue) == null) {
85 >            pool.registerWorker(this);
86 >            q = workQueue;
87 >        }
88 >        return q.poolIndex;
89      }
90  
91      /**
# Line 86 | Line 98 | public class ForkJoinWorkerThread extend
98       * processing tasks.
99       */
100      protected void onStart() {
89        String pref; // replace bootstrap name
90        if (provisionalName.equals(getName()) &&
91            (pref = pool.workerNamePrefix) != null)
92            setName(pref.concat(Long.toString(getId())));
101      }
102  
103      /**
# Line 111 | Line 119 | public class ForkJoinWorkerThread extend
119      public void run() {
120          Throwable exception = null;
121          try {
122 +            pool.registerWorker(this);
123              onStart();
124              pool.runWorker(workQueue);
125          } catch (Throwable ex) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines