Executors 类的 newWorkStealingPool() 方法创建一个 work-stealing 线程池,使用可用处理器的数量作为其目标并行度级别。
用法
public static ExecutorService newWorkStealingPool()
public static ExecutorService newWorkStealingPool(int parallelism)
参数
并行度 - 目标并行度级别
返回
新创建的线程池
抛出
IllegalArgumentException
例子1
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutorsnewWorkStealingPoolExample1 {
public static void main(final String[] arguments) throws InterruptedException {
ExecutorService excr = Executors.newWorkStealingPool();
ThreadPoolExecutor mypool = (ThreadPoolExecutor) Executors.newCachedThreadPool();;
System.out.println("size of mypool:" + mypool.getPoolSize());
excr.submit(new Threadimpl());
excr.submit(new Threadimpl());
System.out.println("Total number threads scheduled):"+ mypool.getTaskCount());
excr.shutdown();
}
static class Threadimpl implements Runnable {
public void run() {
try {
Long num = (long) (Math.random() / 30);
System.out.println("Thread Name:" +Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(num);
System.out.println("after sleep Thread Name:" +Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出:
size of mypool:0 Thread Name:ForkJoinPool-1-worker-5 after sleep Thread Name:ForkJoinPool-1-worker-5 Thread Name:ForkJoinPool-1-worker-3 after sleep Thread Name:ForkJoinPool-1-worker-3 Total number threads scheduled):0
例子1
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutorsnewWorkStealingPoolExample2 {
public static void main(final String[] arguments) throws InterruptedException {
ExecutorService excr = Executors.newWorkStealingPool(4);
ThreadPoolExecutor mypool = (ThreadPoolExecutor) Executors.newCachedThreadPool();;
System.out.println("size of mypool:" + mypool.getPoolSize());
excr.submit(new Threadimpl());
excr.submit(new Threadimpl());
System.out.println("Total number threads scheduled):"+ mypool.getTaskCount());
excr.shutdown();
}
static class Threadimpl implements Runnable {
public void run() {
try {
Long num = (long) (Math.random() / 30);
System.out.println("Thread Name:" +Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(num);
System.out.println("after sleep Thread Name:" +Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出:
size of mypool:0 Thread Name:ForkJoinPool-1-worker-5 after sleep Thread Name:ForkJoinPool-1-worker-5 Thread Name:ForkJoinPool-1-worker-3 after sleep Thread Name:ForkJoinPool-1-worker-3 Total number threads scheduled):0
相关用法
- Java Executor privilegedThreadFactory()用法及代码示例
- Java Executors Callable()用法及代码示例
- Java Executors newScheduledThreadPool()用法及代码示例
- Java Executors newCachedThreadPool()用法及代码示例
- Java Executors defaultThreadFactory()用法及代码示例
- Java Executors newSingleThreadExecutor()用法及代码示例
- Java Executors newFixedThreadPool()用法及代码示例
- Java Enum equals()用法及代码示例
- Java EnumSet range()用法及代码示例
- Java EnumMap remove()用法及代码示例
- Java Enum toString()用法及代码示例
- Java EnumMap hashCode()用法及代码示例
- Java EnumMap get()用法及代码示例
- Java EnumSet complementOf()用法及代码示例
- Java Enum ordinal()用法及代码示例
- Java EnumSet clone()用法及代码示例
- Java EnumMap entrySet()用法及代码示例
- Java EnumMap containsKey()用法及代码示例
- Java EnumMap containsValue(value)用法及代码示例
- Java Enumeration asIterator()用法及代码示例
注:本文由纯净天空筛选整理自 Java Executor newWorkStealingPool() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。