利用ThreadPoolExecutor创建ScheduledExecutorService
时间: 2023-11-09 13:02:02 浏览: 49
可以通过ThreadPoolExecutor的静态方法newScheduledThreadPool(int corePoolSize)来创建ScheduledExecutorService。其中corePoolSize表示线程池中核心线程的数量,即使这些线程处于空闲状态,也不会被回收。
示例代码如下:
```
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
scheduledExecutorService.schedule(() -> System.out.println("延迟执行"), 5, TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
```
上述代码创建了一个核心线程数为5的ScheduledExecutorService,并在5秒后执行一次任务。
相关问题
ThreadPoolExecutor创建线程
要使用ThreadPoolExecutor创建线程池,可以按照以下步骤进行操作:
1. 导入必要的类:
```java
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
```
2. 创建ThreadPoolExecutor实例:
```java
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize);
```
其中,poolSize是线程池的大小,即同时可以执行的任务数量。
3. 向线程池提交任务:
```java
executor.submit(new Runnable() {
public void run() {
// 任务的具体逻辑
}
});
```
可以通过submit方法提交一个Runnable或者Callable类型的任务。
4. 关闭线程池:
```java
executor.shutdown();
```
调用shutdown方法会等待所有已提交的任务执行完成后再关闭线程池。
完整的示例代码如下:
```java
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExample {
public static void main(String[] args) {
int poolSize = 5;
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize);
for (int i = 0; i < 10; i++) {
executor.submit(new Runnable() {
public void run() {
System.out.println("Task executed by thread: " + Thread.currentThread().getName());
}
});
}
executor.shutdown();
}
}
```
在上面的示例中,我们创建了一个大小为5的线程池,然后提交了10个任务给线程池执行。每个任务会打印当前执行任务的线程名字。最后,我们调用shutdown方法关闭线程池。
使用ThreadPoolExecutor可以更好地管理线程池的行为,例如设置线程池的大小、设置任务的拒绝策略等。
创建ScheduledExecutorService的几种方式
以下是创建ScheduledExecutorService的几种方式:
1. 使用Executors类的newScheduledThreadPool方法创建一个固定大小的线程池:
```java
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
```
2. 使用Executors类的newSingleThreadScheduledExecutor方法创建一个只有一个线程的线程池:
```java
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
```
3. 使用Executors类的newScheduledThreadPool方法创建一个可缓存的线程池,线程数根据需要自动增加或减少:
```java
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(0);
```
4. 使用ThreadPoolExecutor类的构造方法创建一个自定义的ScheduledExecutorService:
```java
ScheduledExecutorService scheduledExecutorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)