Executors 类的 newScheduledThreadPool() 方法创建了一个线程池,该线程池可以安排命令在给定延迟后运行或定期执行。
用法
Public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)
参数
corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态
threadFactory - 执行程序创建新线程时使用的工厂
返回
新创建的调度线程池
抛出
NullPointerException
IllegalArgumentException
例子1
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutorsnewSheduledThreadPoollExample1 {
public static void main(final String[] arguments) throws InterruptedException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 3 );
ThreadFactory ThreadFactory = Executors.defaultThreadFactory();
System.out.println(scheduler.getClass());
ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory);
ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
System.out.println("size of mypool:" + mypool.getPoolSize());
excr.submit(new Threadimpl());
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();
}
}
}
}
输出:
class java.util.concurrent.ScheduledThreadPoolExecutor size of mypool:0 Thread Name:pool-2-thread-1 after sleep Thread Name:pool-2-thread-1
例子2
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutorsnewSheduledThreadPoollExample2 {
public static void main(final String[] arguments) throws InterruptedException {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 3 );
ThreadFactory ThreadFactory = Executors.defaultThreadFactory();
System.out.println(scheduler.getClass());
ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory);
ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
System.out.println("size of mypool:" + mypool.getPoolSize());
System.out.println(scheduler.isShutdown());
System.out.println(scheduler.getClass());
excr.shutdown();
}
}
输出:
class java.util.concurrent.ScheduledThreadPoolExecutor size of mypool:0 false class java.util.concurrent.ScheduledThreadPoolExecutor
相关用法
- Java Executors newSingleThreadExecutor()用法及代码示例
- Java Executors newCachedThreadPool()用法及代码示例
- Java Executors newFixedThreadPool()用法及代码示例
- Java Executors Callable()用法及代码示例
- Java Executors defaultThreadFactory()用法及代码示例
- Java Executor newWorkStealingPool()用法及代码示例
- Java Executor privilegedThreadFactory()用法及代码示例
- 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 Executors newScheduledThreadPool() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。