更多请访问 www.itkc8.com
public String list2Str(List<String> list, final int nThreads) throws Exception {
if (CollectionUtils.isEmpty(list)) {
return null;
}
int size = list.size();
System.out.println("处理key的大小:"+list.size());
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<Future<List<GpsUploadVO>>> futures = new ArrayList<>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List<String> subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
Callable<List<GpsUploadVO>> task = () -> {
List<GpsUploadVO> gpss = new ArrayList<>();
for (String str : subList) {
List<GpsUploadVO> range = redisTemplate.opsForList().range(str, 0, 4);
gpss.addAll(range);
}
return gpss;
};
futures.add(executorService.submit(task));
}
List<GpsUploadVO> lt = new ArrayList<>();
for (Future<List<GpsUploadVO>> future : futures) {
List<GpsUploadVO> ls = future.get();
lt.addAll(ls);
}
executorService.shutdown();
System.out.println("list的大小:"+lt.size());
Map<String, List<GpsUploadVO>> groupBy = lt.stream().collect(Collectors.groupingBy(GpsUploadVO::getPhoneNum));
System.out.println("分组大小:"+groupBy.size());
return null;
}
public String list2Str(List<String> list, final int nThreads) throws Exception {
if (list == null || list.isEmpty()) {
return null;
}
StringBuffer ret = new StringBuffer();
int size = list.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<Future<String>> futures = new ArrayList<Future<String>>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List<String> subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
Callable<String> task = new Callable<String>() {
@Override
public String call() throws Exception {
StringBuffer sb = new StringBuffer();
for (String str : subList) {
sb.append(str);
}
return sb.toString();
}
};
futures.add(executorService.submit(task));
}
for (Future<String> future : futures) {
ret.append(future.get());
}
executorService.shutdown();
return ret.toString();
}
}