public ForkJoinWorkerThread newThread(ForkJoinPool pool) { return new ForkJoinWorkerThread(pool); } }
/** * This method is required to be public, but should never be * called explicitly. It performs the main run loop to execute * {@link ForkJoinTask}s. */ public void run() { Throwable exception = null; try { onStart(); pool.runWorker(workQueue); } catch (Throwable ex) { exception = ex; } finally { try { onTermination(exception); } catch (Throwable ex) { if (exception == null) exception = ex; } finally { pool.deregisterWorker(this, exception); } } } }
/** * Creates a ForkJoinWorkerThread operating in the given pool. * * @param pool the pool this thread works in * @throws NullPointerException if pool is null */ protected ForkJoinWorkerThread(ForkJoinPool pool) { super(pool.nextWorkerName()); setDaemon(true); Thread.UncaughtExceptionHandler ueh = pool.ueh; if (ueh != null) setUncaughtExceptionHandler(ueh); this.pool = pool; pool.registerWorker(this.workQueue = new ForkJoinPool.WorkQueue (pool, this, pool.localMode)); }
else if (ws != null && (n = ws.length) > 1) { ForkJoinWorkerThread w; int r = nextSeed(); // cheap version of FJP.scan int steps = n << 1; for (int i = 0; i < steps; ++i) { p.addActiveCount(1); if ((t = (v != this) ? v.deqTask() : locallyFifo ? locallyDeqTask() : popTask()) != null) { currentSteal = t; t.doExec();
/** * Gets and removes a local or stolen task. * * @return a task, if available */ final ForkJoinTask<?> pollTask() { ForkJoinWorkerThread[] ws; ForkJoinTask<?> t = pollLocalTask(); if (t != null || (ws = pool.workers) == null) return t; int n = ws.length; // cheap version of FJP.scan int steps = n << 1; int r = nextSeed(); int i = 0; while (i < steps) { ForkJoinWorkerThread w = ws[(i++ + r) & (n - 1)]; if (w != null && w.queueBase != w.queueTop && w.queue != null) { if ((t = w.deqTask()) != null) return t; i = 0; } } return null; }
w.terminate = true; if (pass > 0) { w.cancelTasks(); if (pass > 1 && !w.isInterrupted()) { try { w.interrupt(); } catch (SecurityException ignore) {
/** * Tries to create and start a worker */ private void addWorker() { Throwable ex = null; ForkJoinWorkerThread wt = null; try { if ((wt = factory.newThread(this)) != null) { wt.start(); return; } } catch (Throwable e) { ex = e; } deregisterWorker(wt, ex); // adjust counts etc on failure }
/** * Drains tasks to given collection c. * * @return the number of tasks drained */ final int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { int n = 0; while (queueBase != queueTop) { ForkJoinTask<?> t = deqTask(); if (t != null) { c.add(t); ++n; } } return n; }
for (ForkJoinWorkerThread w : ws) if (w != null) count += w.drainTasksTo(c);
/** * Performs cleanup associated with termination of this worker * thread. If you override this method, you must invoke * {@code super.onTermination} at the end of the overridden method. * * @param exception the exception causing this thread to abort due * to an unrecoverable error, or {@code null} if completed normally */ protected void onTermination(Throwable exception) { try { terminate = true; cancelTasks(); pool.deregisterWorker(this, exception); } catch (Throwable ex) { // Shouldn't ever happen if (exception == null) // but if so, at least rethrown exception = ex; } finally { if (exception != null) UNSAFE.throwException(exception); } }
/** * Tries to create and start a worker; minimally rolls back counts * on failure. */ private void addWorker() { Throwable ex = null; ForkJoinWorkerThread t = null; try { t = factory.newThread(this); } catch (Throwable e) { ex = e; } if (t == null) { // null or exceptional factory return long c; // adjust counts do {} while (!UNSAFE.compareAndSwapLong (this, ctlOffset, c = ctl, (((c - AC_UNIT) & AC_MASK) | ((c - TC_UNIT) & TC_MASK) | (c & ~(AC_MASK|TC_MASK))))); // Propagate exception if originating from an external caller if (!tryTerminate(false) && ex != null && !(Thread.currentThread() instanceof ForkJoinWorkerThread)) UNSAFE.throwException(ex); } else t.start(); }
/** * Removes and cancels all tasks in queue. Can be called from any * thread. */ final void cancelTasks() { ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks if (cj != null && cj.status >= 0) cj.cancelIgnoringExceptions(); ForkJoinTask<?> cs = currentSteal; if (cs != null && cs.status >= 0) cs.cancelIgnoringExceptions(); while (queueBase != queueTop) { ForkJoinTask<?> t = deqTask(); if (t != null) t.cancelIgnoringExceptions(); } }
/** * Creates a ForkJoinWorkerThread operating in the given pool. * * @param pool the pool this thread works in * @throws NullPointerException if pool is null */ protected ForkJoinWorkerThread(ForkJoinPool pool) { super(pool.nextWorkerName()); this.pool = pool; int k = pool.registerWorker(this); poolIndex = k; eventCount = ~k & SMASK; // clear wait count locallyFifo = pool.locallyFifo; Thread.UncaughtExceptionHandler ueh = pool.ueh; if (ueh != null) setUncaughtExceptionHandler(ueh); setDaemon(true); }
/** * This method is required to be public, but should never be * called explicitly. It performs the main run loop to execute * {@link ForkJoinTask}s. */ public void run() { Throwable exception = null; try { onStart(); pool.work(this); } catch (Throwable ex) { exception = ex; } finally { onTermination(exception); } }
public ForkJoinWorkerThread newThread(ForkJoinPool pool) { return new ForkJoinWorkerThread(pool); } }