ExecutorService executorService = Executors.newFixedThreadPool(threadCount);改为手动创建线程
时间: 2023-07-27 10:12:48 浏览: 85
可以使用如下代码手动创建线程并执行任务:
```
int threadCount = 10; // 线程数量
List<Thread> threads = new ArrayList<>(threadCount);
for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread(() -> {
// 执行任务代码
});
threads.add(thread);
thread.start(); // 启动线程
}
// 等待所有线程执行完成
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
```
需要注意的是,手动创建线程需要自己管理线程的生命周期和线程池大小,如果线程数量过多可能会导致系统资源消耗较大,因此建议使用线程池来管理线程。
相关问题
List<UserEo> userEoList = comDataComponent.getUserListByUid();int batchSize = 1000; // 每批次处理1000个用户int threadCount = (userEoList.size() + batchSize - 1) / batchSize; // 计算需要开启的线程数ExecutorService executorService = Executors.newFixedThreadPool(threadCount);for (int i = 0; i < threadCount; i++) { int start = i * batchSize; int end = Math.min(start + batchSize, userEoList.size()); List<UserEo> subList = userEoList.subList(start, end); executorService.submit(() -> { for (UserEo userEo : subList) { List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode()); String redisKey = SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid(); redisTemplate.executePipelined((RedisCallback<Object>) connection -> { connection.set(redisTemplate.getKeySerializer().serialize(redisKey), redisTemplate.getValueSerializer().serialize(ObjectUtil.toString(userDataPermissionSchema))); return null; }); } });}executorService.shutdown();executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);改为手动创建线程池
可以将原来的代码改为手动创建线程池的方式,如下所示:
```
List<UserEo> userEoList = comDataComponent.getUserListByUid();
int batchSize = 1000; // 每批次处理1000个用户
int threadCount = (userEoList.size() + batchSize - 1) / batchSize; // 计算需要开启的线程数
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
int start = i * batchSize;
int end = Math.min(start + batchSize, userEoList.size());
List<UserEo> subList = userEoList.subList(start, end);
Thread thread = new Thread(() -> {
for (UserEo userEo : subList) {
List<DataPermissionSchema> userDataPermissionSchema = getUserDataPermissionSchema(userEo.getUserUid(), userEo.getOrgCode());
String redisKey = SystemsConstant.DATA_PERMISSION_SCHEMA + userEo.getUserUid();
redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
connection.set(redisTemplate.getKeySerializer().serialize(redisKey), redisTemplate.getValueSerializer().serialize(ObjectUtil.toString(userDataPermissionSchema)));
return null;
});
}
});
executorService.submit(thread);
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
```
这个代码中,我们手动创建了线程池,并且使用了 `Thread` 类来创建线程。我们将任务放入线程中运行,并且使用 `executorService.submit(thread)` 方法将线程提交到线程池中。最后,我们关闭线程池,并等待所有任务执行完毕。
Executors.newFixedThreadPool 线程池设置名称
你可以使用以下代码来创建一个带有名称的固定大小线程池:
```java
ExecutorService executor = Executors.newFixedThreadPool(n, new ThreadFactory() {
private AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "MyThreadPool-" + count.incrementAndGet());
}
});
```
其中,`n` 是线程池的大小,`ThreadFactory` 是用于为线程池中的每个线程创建一个对应的线程对象的工厂类。在上述代码中,我们创建了一个匿名内部类实现了 `ThreadFactory` 接口,并在 `newThread` 方法中为每个线程设置了名称。名称的格式是 `MyThreadPool-1`、`MyThreadPool-2`、`MyThreadPool-3` 等等。
阅读全文