ScheduledThreadPoolExecutor 构造方法有哪些
时间: 2023-12-07 16:37:26 浏览: 82
源码解析文件ScheduledThreadPoolExecutor
根据引用[1]所述,ScheduledThreadPoolExecutor类是ThreadPoolExecutor类的子类,因此ScheduledThreadPoolExecutor类的构造方法实际上调用的是ThreadPoolExecutor类的构造方法。ScheduledThreadPoolExecutor类的构造方法如下:
1. ScheduledThreadPoolExecutor(int corePoolSize)
- 创建一个具有给定核心线程数的新ScheduledThreadPoolExecutor。
2. ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory)
- 创建一个具有给定核心线程数和线程工厂的新ScheduledThreadPoolExecutor。
3. ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler)
- 创建一个具有给定核心线程数和拒绝执行处理程序的新ScheduledThreadPoolExecutor。
4. ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
- 创建一个具有给定核心线程数、线程工厂和拒绝执行处理程序的新ScheduledThreadPoolExecutor。
其中,corePoolSize表示线程池中的核心线程数;threadFactory表示用于创建新线程的工厂;RejectedExecutionHandler表示当线程池无法执行任务时的处理程序。
示例代码如下:
```java
// 创建一个具有给定核心线程数的新ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor executor1 = new ScheduledThreadPoolExecutor(5);
// 创建一个具有给定核心线程数和线程工厂的新ScheduledThreadPoolExecutor
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ScheduledThreadPoolExecutor executor2 = new ScheduledThreadPoolExecutor(5, threadFactory);
// 创建一个具有给定核心线程数和拒绝执行处理程序的新ScheduledThreadPoolExecutor
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ScheduledThreadPoolExecutor executor3 = new ScheduledThreadPoolExecutor(5, handler);
// 创建一个具有给定核心线程数、线程工厂和拒绝执行处理程序的新ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor executor4 = new ScheduledThreadPoolExecutor(5, threadFactory, handler);
```
阅读全文