5

Java 8 と組み合わせて使用​​される保留中のタスクのキューはありますExecutors.newWorkStealingPool()か?

たとえば、使用可能なコア数が 2 で、Executors.newWorkStealingPool()2 つのタスクが既に実行されているため空であるとします。次に、3 番目のタスクがワークスティーリング エグゼキューターに送信されるとどうなるでしょうか? キューされていますか?もしそうなら、そのキューにある場合、その境界は何ですか?

前もって感謝します。

4

2 に答える 2

5

Java 8 の Executors.newWorkStealingPool() と組み合わせて使用​​される保留中のタスクのキューはありますか?

はい、すべてのスレッドは独自の両端キューでサポートされています。1 つのスレッドがそのタスクを完了すると、他のスレッドの両端キューからタスクを取得して実行します。

もしそうなら、そのキューにある場合、その境界は何ですか?

キューの最大サイズは、次の数によって制限されます。static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M

キューがいっぱいになると、未チェックの例外がスローされます。RejectedExecutionException("Queue capacity exceeded")

于 2016-04-22T15:27:40.207 に答える
3

ExecutorsとForkJoinPool のgrepcodeから

Executors. newWorkStealingPool戻り値ForkJoinPool

実行者:

 public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

ForkJoinPool:

public ForkJoinPool(int parallelism,
                        ForkJoinWorkerThreadFactory factory,
                        UncaughtExceptionHandler handler,
                        boolean asyncMode) {
        this(checkParallelism(parallelism),
             checkFactory(factory),
             handler,
             asyncMode ? FIFO_QUEUE : LIFO_QUEUE,
             "ForkJoinPool-" + nextPoolId() + "-worker-");
        checkPermission();
    }

オンexecute():

public void execute(ForkJoinTask<?> task) {
        if (task == null)
            throw new NullPointerException();
        externalPush(task);
    }

externalPush呼び出し、その実装で詳細をexternalSubmit確認できます。WorkQueue

外部送信:

// 外部操作

/**
 * Full version of externalPush, handling uncommon cases, as well
 * as performing secondary initialization upon the first
 * submission of the first task to the pool.  It also detects
 * first submission by an external thread and creates a new shared
 * queue if the one at index if empty or contended.
 *
 * @param task the task. Caller must ensure non-null.

 */

WorkQueueクラスでキューサイズの詳細を見つけることができます

 static final class WorkQueue {

ドキュメントWokrQueue:

/**
     * Queues supporting work-stealing as well as external task
     * submission. See above for descriptions and algorithms.
     * Performance on most platforms is very sensitive to placement of
     * instances of both WorkQueues and their arrays -- we absolutely
     * do not want multiple WorkQueue instances or multiple queue
     * arrays sharing cache lines. The @Contended annotation alerts
     * JVMs to try to keep instances apart.
     */
    @sun.misc.Contended

 /**
     * Capacity of work-stealing queue array upon initialization.
     * Must be a power of two; at least 4, but should be larger to
     * reduce or eliminate cacheline sharing among queues.
     * Currently, it is much larger, as a partial workaround for
     * the fact that JVMs often place arrays in locations that
     * share GC bookkeeping (especially cardmarks) such that
     * per-write accesses encounter serious memory contention.
     */
    static final int INITIAL_QUEUE_CAPACITY = 1 << 13;

    /**
     * Maximum size for queue arrays. Must be a power of two less
     * than or equal to 1 << (31 - width of array entry) to ensure
     * lack of wraparound of index calculations, but defined to a
     * value a bit less than this to help users trap runaway
     * programs before saturating systems.
     */
    static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M
于 2016-04-22T16:08:35.820 に答える