java ThreadPoolExecutor 创建5个线程池每个线程池平均分配list里面的数据GeoResultsRedisGeoCommandsGeoLocationString大小有500条
可以使用以下代码创建5个线程池,并将GeoResults<RedisGeoCommands.GeoLocation
int numThreads = 5;
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads);
// 每个线程池处理的元素数量
int batchSize = (int) Math.ceil(500.0 / numThreads);
for (int i = 0; i < numThreads; i++) {
int startIndex = i * batchSize;
int endIndex = Math.min(startIndex + batchSize, 500);
List<RedisGeoCommands.GeoLocation<String>> batchList = geoResults.getContent().subList(startIndex, endIndex);
executor.execute(() -> {
// 处理batchList中的元素
});
}
executor.shutdown();
该代码使用Executors.newFixedThreadPool方法创建一个具有固定线程数的线程池。然后,它将GeoResults<RedisGeoCommands.GeoLocation
原文地址: https://www.cveoy.top/t/topic/bVSv 著作权归作者所有。请勿转载和采集!