ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
时间: 2023-11-22 14:46:21 浏览: 36
ThreadPoolExecutor是Java中的一个线程池实现类,它可以用来管理和调度多个线程。在创建ThreadPoolExecutor时,需要指定一些参数,例如核心线程数、最大线程数、线程空闲时间等。其中,核心线程数是指线程池中一直存在的线程数量,最大线程数是指线程池中最多可以存在的线程数量。当线程池中的线程数量超过核心线程数时,新的任务会被放入阻塞队列中等待执行。如果阻塞队列已满,且线程池中的线程数量还没有达到最大线程数,那么会创建新的线程来执行任务。如果线程池中的线程数量已经达到最大线程数,那么新的任务会被拒绝执行。
在这个例子中,创建了一个ThreadPoolExecutor对象,它有10个核心线程,最多可以存在20个线程,线程空闲时间为60秒,任务队列使用的是LinkedBlockingQueue。这个线程池可以用来执行多个任务,当任务数量超过核心线程数时,新的任务会被放入任务队列中等待执行。如果任务队列已满,且线程池中的线程数量还没有达到最大线程数,那么会创建新的线程来执行任务。如果线程池中的线程数量已经达到最大线程数,那么新的任务会被拒绝执行。
相关问题
package com.automic.swp.irri.api.utils; import java.util.concurrent.*; /** * 创建服务器线程池 * * @author zhengzhuanzi <url>http://www.z3pipe.com</url> */ public class ThreadPoolUtil { private ThreadPoolUtil() { } /** * make server thread pool * * @param serverType 服务器类型 * @return */ public static ThreadPoolExecutor makeServerThreadPool(final String serverType) { //系统处理器核数 int corePoolSize = Runtime.getRuntime().availableProcessors(); return makeServerThreadPool(serverType, corePoolSize, corePoolSize * 4); } /** * make server thread pool * * @param serverType 服务器类型 * @return */ public static ThreadPoolExecutor makeServerThreadPool(final String serverType, int corePoolSize, int maxPoolSize) { return makeServerThreadPool(serverType, corePoolSize, maxPoolSize, 60, 1000); } /** * make server thread pool * * @param serverType 服务器类型 * @return */ public static ThreadPoolExecutor makeServerThreadPool(final String serverType, int corePoolSize, int maxPoolSize, long keepAliveTime, int capacity) { ThreadFactory threadFactory = new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, serverType + "-serverHandlerPool-" + r.hashCode()); } }; RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { throw new RuntimeException("" + serverType + " Thread pool is EXHAUSTED!"); } }; return new ThreadPoolExecutor( corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<>(capacity), threadFactory , rejectedExecutionHandler ); } }解释一下这个类教我怎么用
这个类提供了一个静态方法 `makeServerThreadPool`,它可以用来创建一个服务器线程池。你可以通过传入不同的参数来定制线程池的大小和其他属性。
如果你只想使用默认的参数,可以直接调用不带参数的 `makeServerThreadPool` 方法。如果你想自定义线程池的大小等属性,可以调用带参数的 `makeServerThreadPool` 方法,并传入对应的参数值。
例如,要创建一个名为 `myServer` 的服务器线程池,可以这样使用:
```java
ThreadPoolExecutor threadPool = ThreadPoolUtil.makeServerThreadPool("myServer", 10, 20, 120, 200);
```
这将创建一个核心线程数为 10,最大线程数为 20,线程空闲时间为 120 秒,任务队列容量为 200 的线程池。你可以将这个线程池用于你的服务器程序中。
springboot项目中new ThreadPoolExecutor.CallerRunsPolicy()如何自定义CallerRunsPolicy()
在Spring Boot项目中,可以通过自定义`CallerRunsPolicy`来实现自定义的拒绝策略。`CallerRunsPolicy`是`ThreadPoolExecutor`的一种拒绝策略,当线程池无法接受新任务时,会将任务交给调用线程来执行。
要自定义`CallerRunsPolicy`,可以按照以下步骤进行操作:
1. 创建一个类,继承`ThreadPoolExecutor.CallerRunsPolicy`,并重写`rejectedExecution`方法。在该方法中,可以实现自定义的拒绝逻辑。
```java
public class CustomCallerRunsPolicy extends ThreadPoolExecutor.CallerRunsPolicy {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 自定义的拒绝逻辑
// ...
super.rejectedExecution(r, executor);
}
}
```
2. 在创建线程池时,使用自定义的`CallerRunsPolicy`作为拒绝策略。
```java
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new CustomCallerRunsPolicy()
);
```
通过以上步骤,你可以自定义`CallerRunsPolicy`来实现你所需的拒绝策略。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)