首页 > 编程语言 > 详细

java线程池应用

时间:2020-06-20 12:02:19      阅读:57      评论:0      收藏:0      [点我收藏+]
//多个线程跑同一个任务,参数不同 
int thread = Integer.parseInt(2);
            int threadSize = 3000;
            ExecutorService eService = Executors.newFixedThreadPool(thread); //创建一个线程池
            List<Callable<String>> cList = new ArrayList<>();  //定义添加线程的集合
            Callable<String> task = null;  //创建单个线程
            List<ResearchLogistics> sList = null;
            for (int i = 0; i < thread; i++) {  //根据线程数去取数据和创建线程
                if (i == thread - 1) {
                    sList = dataList.subList(i * threadSize, dataList.size());
                } else {
                    sList = dataList.subList(i * threadSize, (i + 1) * threadSize);
                }
                final List<ResearchLogistics> nowList = sList;
                //创建单个线程
                task = new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        try {
                            coordinateUtil(nowList, regionList, provinceList, cityList);
                        } catch (Exception e) {
                            e.printStackTrace();
                            return "失败";
                        }
                        return "成功";
                    }
                };
                cList.add(task); //添加线程
            }
            List<Future<String>> results = eService.invokeAll(cList); //执行所有创建的线程,并获取返回值(会把所有线程的返回值都返回)

 

java线程池应用

原文:https://www.cnblogs.com/tongsi/p/13167839.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!