java.util.concurrent.ForkJoinPool.scan()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(144)

本文整理了Java中java.util.concurrent.ForkJoinPool.scan()方法的一些代码示例,展示了ForkJoinPool.scan()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ForkJoinPool.scan()方法的具体详情如下:
包路径:java.util.concurrent.ForkJoinPool
类名称:ForkJoinPool
方法名:scan

ForkJoinPool.scan介绍

[英]Scans for and, if found, returns one task, else possibly inactivates the worker. This method operates on single reads of volatile state and is designed to be re-invoked continuously, in part because it returns upon detecting inconsistencies, contention, or state changes that indicate possible success on re-invocation. The scan searches for tasks across queues (starting at a random index, and relying on registerWorker to irregularly scatter them within array to avoid bias), checking each at least twice. The scan terminates upon either finding a non-empty queue, or completing the sweep. If the worker is not inactivated, it takes and returns a task from this queue. Otherwise, if not activated, it signals workers (that may include itself) and returns so caller can retry. Also returns for true if the worker array may have changed during an empty scan. On failure to find a task, we take one of the following actions, after which the caller will retry calling this method unless terminated. * If pool is terminating, terminate the worker. * If not already enqueued, try to inactivate and enqueue the worker on wait queue. Or, if inactivating has caused the pool to be quiescent, relay to idleAwaitWork to possibly shrink pool. * If already enqueued and none of the above apply, possibly park awaiting signal, else lingering to help scan and signal. * If a non-empty queue discovered or left as a hint, help wake up other workers before return.
[中]扫描并(如果找到)返回一个任务,否则可能会禁用工作进程。此方法对易失性状态的单次读取进行操作,并设计为连续重新调用,部分原因是它在检测到不一致、争用或状态更改时返回,这些更改指示重新调用可能成功。扫描在队列中搜索任务(从随机索引开始,依靠registerWorker将任务不规则地分散在数组中以避免偏差),每个任务至少检查两次。扫描在找到非空队列或完成扫描时终止。如果工作进程未处于非激活状态,它将从此队列中获取并返回一个任务。否则,若未激活,它会向工作者(可能包括自身)发出信号并返回,以便调用方可以重试。如果工作阵列在空扫描期间可能已更改,则还返回true。如果找不到任务,我们将执行以下操作之一,之后调用方将重试调用此方法,除非终止。*如果池正在终止,请终止工作进程。*如果尚未排队,请尝试停用等待队列上的工作进程并将其排队。或者,如果停用导致池处于静止状态,则中继到idleAwaitWork以可能收缩池。*如果已经排队且上述条件均不适用,则可能需要停车等待信号,否则需要逗留以帮助扫描和发送信号。*如果发现或留下非空队列作为提示,请在返回前帮助唤醒其他工作人员。

代码示例

代码示例来源:origin: robovm/robovm

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Top-level loop for worker threads: On each step: if the
 * previous step swept through all queues and found no tasks, or
 * there are excess threads, then possibly blocks. Otherwise,
 * scans for and, if found, executes a task. Returns when pool
 * and/or worker terminate.
 *
 * @param w the worker
 */
final void work(ForkJoinWorkerThread w) {
  boolean swept = false;                // true on empty scans
  long c;
  while (!w.terminate && (int)(c = ctl) >= 0) {
    int a;                            // active count
    if (!swept && (a = (int)(c >> AC_SHIFT)) <= 0)
      swept = scan(w, a);
    else if (tryAwaitWork(w, c))
      swept = false;
  }
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Top-level loop for worker threads: On each step: if the
 * previous step swept through all queues and found no tasks, or
 * there are excess threads, then possibly blocks. Otherwise,
 * scans for and, if found, executes a task. Returns when pool
 * and/or worker terminate.
 *
 * @param w the worker
 */
final void work(ForkJoinWorkerThread w) {
  boolean swept = false;                // true on empty scans
  long c;
  while (!w.terminate && (int)(c = ctl) >= 0) {
    int a;                            // active count
    if (!swept && (a = (int)(c >> AC_SHIFT)) <= 0)
      swept = scan(w, a);
    else if (tryAwaitWork(w, c))
      swept = false;
  }
}

代码示例来源:origin: org.codehaus.jsr166-mirror/jsr166

/**
 * Top-level loop for worker threads: On each step: if the
 * previous step swept through all queues and found no tasks, or
 * there are excess threads, then possibly blocks. Otherwise,
 * scans for and, if found, executes a task. Returns when pool
 * and/or worker terminate.
 *
 * @param w the worker
 */
final void work(ForkJoinWorkerThread w) {
  boolean swept = false;                // true on empty scans
  long c;
  while (!w.terminate && (int)(c = ctl) >= 0) {
    int a;                            // active count
    if (!swept && (a = (int)(c >> AC_SHIFT)) <= 0)
      swept = scan(w, a);
    else if (tryAwaitWork(w, c))
      swept = false;
  }
}

代码示例来源:origin: MobiVM/robovm

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: FlexoVM/flexovm

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: com.bugvm/bugvm-rt

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: com.gluonhq/robovm-rt

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: ibinti/bugvm

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
 */
final void runWorker(WorkQueue w) {
  w.growArray(); // allocate queue
  do { w.runTask(scan(w)); } while (w.qlock >= 0);
}

代码示例来源:origin: stackoverflow.com

"ForkJoinPool.commonPool-worker-2@710" daemon prio=5 tid=0xe nid=NA runnable java.lang.Thread.State: RUNNABLE
at java.util.stream.Stream$1.next(Stream.java:1033)
 at java.util.Spliterators$IteratorSpliterator.trySplit(Spliterators.java:1784)
 at java.util.stream.AbstractShortCircuitTask.compute(AbstractShortCircuitTask.java:114)
 at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
 at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
 at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:902)
 at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1689)
 at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1644)
 at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)`

相关文章

ForkJoinPool类方法