20 Ocak 2021 Çarşamba

Executors.newWorkStealingPool metodu

Giriş
Açıklaması şöyle. Java 8 ile geliyor. Altta ForkJoinPool kullanır. Bu yüzden sadece "recursive divide and conquer" tarzı işler için uygundur
Creates a work-stealing thread pool using all available processors as its target parallelism level.
newWorkStealingPool metodu
İmzası şöyle.
public static ExecutorService newWorkStealingPool();
Kodu şöyle
/**
* Creates a work-stealing thread pool using all
* {@link Runtime#availableProcessors available processors}
* as its target parallelism level.
* @return the newly created thread pool
* @see #newWorkStealingPool(int)
* @since 1.8
*/
public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
                            ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                            null, true);
}
newWorkStealingPool metodu - int parallelism
Kodu şöyle
/**
* Creates a thread pool that maintains enough threads to support
* the given parallelism level, and may use multiple queues to
* reduce contention. The parallelism level corresponds to the
* maximum number of threads actively engaged in, or available to
* engage in, task processing. The actual number of threads may
* grow and shrink dynamically. A work-stealing pool makes no
* guarantees about the order in which submitted tasks are
* executed.
*
* @param parallelism the targeted parallelism level
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code parallelism <= 0}
* @since 1.8
*/
public static ExecutorService newWorkStealingPool(int parallelism) {
    return new ForkJoinPool(parallelism,
                            ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                            null, true);
}
Örnek
Şöyle yaparız.
ExecutorService pool = Executors.newWorkStealingPool(10);

Hiç yorum yok:

Yorum Gönder