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.1 by dl, Mon Aug 13 15:52:33 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 +     * Sequence number for creating worker Names
45 +     */
46 +    private static final AtomicInteger threadNumber = new AtomicInteger();
47  
48      /**
49       * Creates a ForkJoinWorkerThread operating in the given pool.
# Line 37 | Line 52 | public class ForkJoinWorkerThread extend
52       * @throws NullPointerException if pool is null
53       */
54      protected ForkJoinWorkerThread(ForkJoinPool pool) {
55 <        super(pool.nextWorkerName());
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);
45        this.pool = pool;
46        pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue
47                            (pool, this, pool.localMode));
61      }
62  
63      /**
# Line 66 | 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 100 | 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