我是靠谱客的博主 知性哈密瓜,这篇文章主要介绍多线程代码更多请访问 www.itkc8.com,现在分享给大家,希望可以做个参考。

更多请访问 www.itkc8.com

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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(); } }

 

最后

以上就是知性哈密瓜最近收集整理的关于多线程代码更多请访问 www.itkc8.com的全部内容,更多相关多线程代码更多请访问内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部