// 创建一个线程池,根据CPU核心数来创建 int corePoolSize = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(corePoolSize); // 将任务拆分成多个子任务 List<List<UserEo>> subUserEoList = ListUtils.splitList(userEoList, corePoolSize); // 提交子任务到线程池中执行 for (List<UserEo> subList : subUserEoList) { executorService.execute(() -> { for (UserEo userEo : subList) { List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode()); redisTemplate.opsForValue().set(SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid(), ObjectUtil.toString(userDataPermissionSchema)); } });} // 关闭线程池 executorService.shutdown();编译错误说手动创建线程池更好
时间: 2024-02-03 08:11:36 浏览: 36
手动创建线程池确实比使用Executors类中提供的静态方法更好。这是因为Executors类中的静态方法可能会使用一些默认的配置,这些配置并不一定适用于你的具体场景。另外,手动创建线程池可以让你更好地控制线程池的参数,以便更好地适应你的具体需求。
以下是手动创建线程池的示例代码:
```java
int corePoolSize = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize, // 核心线程数
corePoolSize, // 最大线程数
0L, TimeUnit.MILLISECONDS, // 空闲线程存活时间
new LinkedBlockingQueue<Runnable>(), // 任务队列
Executors.defaultThreadFactory(), // 线程工厂
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
);
```
在这个示例中,我们使用了ThreadPoolExecutor类来手动创建线程池。它的构造函数需要传入一些参数,包括核心线程数、最大线程数、任务队列、线程工厂和拒绝策略等。你可以根据自己的需求来设置这些参数。
相关问题
// 创建一个线程池,根据CPU核心数来创建 int corePoolSize = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = Executors.newFixedThreadPool(corePoolSize); // 将任务拆分成多个子任务 List<List<UserEo>> subUserEoList = ListUtils.splitList(userEoList, corePoolSize); // 提交子任务到线程池中执行 for (List<UserEo> subList : subUserEoList) { executorService.execute(() -> { for (UserEo userEo : subList) { List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode()); redisTemplate.opsForValue().set(SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid(), ObjectUtil.toString(userDataPermissionSchema)); } });} // 关闭线程池 executorService.shutdown();改为手动创建线程池
如果你想手动创建线程池,你可以使用以下代码:
```
// 创建一个线程池,根据CPU核心数来创建
int corePoolSize = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executorService = new ThreadPoolExecutor(
corePoolSize, // 核心线程数
corePoolSize, // 最大线程数
0L, TimeUnit.MILLISECONDS, // 空闲线程存活时间
new LinkedBlockingQueue<>(), // 任务队列
Executors.defaultThreadFactory(), // 线程工厂
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
);
// 将任务拆分成多个子任务
List<List<UserEo>> subUserEoList = ListUtils.splitList(userEoList, corePoolSize);
// 提交子任务到线程池中执行
for (List<UserEo> subList : subUserEoList) {
executorService.execute(() -> {
for (UserEo userEo : subList) {
List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode());
redisTemplate.opsForValue().set(SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid(), ObjectUtil.toString(userDataPermissionSchema));
}
});
}
// 关闭线程池
executorService.shutdown();
```
这里使用了 `ThreadPoolExecutor` 类手动创建线程池,并设置了一些参数,例如核心线程数、最大线程数、空闲线程存活时间等。还设置了线程工厂和拒绝策略。然后使用 `execute` 方法提交任务到线程池中执行,最后调用 `shutdown` 方法关闭线程池。
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 的线程池。你可以将这个线程池用于你的服务器程序中。