java线程池应用


//多个线程跑同一个任务,参数不同 
int thread = Integer.parseInt(2);
            int threadSize = 3000;
            ExecutorService eService = Executors.newFixedThreadPool(thread); //创建一个线程池
            List> cList = new ArrayList<>();  //定义添加线程的集合
            Callable task = null;  //创建单个线程
            List 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 nowList = sList;
                //创建单个线程
                task = new Callable() {
                    @Override
                    public String call() throws Exception {
                        try {
                            coordinateUtil(nowList, regionList, provinceList, cityList);
                        } catch (Exception e) {
                            e.printStackTrace();
                            return "失败";
                        }
                        return "成功";
                    }
                };
                cList.add(task); //添加线程
            }
            List> results = eService.invokeAll(cList); //执行所有创建的线程,并获取返回值(会把所有线程的返回值都返回)