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

概述

更多请访问 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();  
        }  
    } 

 

最后

以上就是知性哈密瓜为你收集整理的多线程代码更多请访问 www.itkc8.com的全部内容,希望文章能够帮你解决多线程代码更多请访问 www.itkc8.com所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部