【Java-多线程】Java-多线程-Callable

ExecutorService executorService = null;
        try {
          
   
        	//设置多线程数量
            executorService = Executors.newFixedThreadPool(partitionParallelism);
            //定义集合为了实现线程阻塞
            ArrayList<Future<Boolean>> booleans = new ArrayList<>();
            //遍历分区信息
            //for循环后进行executorService.submit操作,并重写Callable方法,在Callable方法内进行循环体内的逻辑操作。
            for (int i = 0; i < 10; i++) {
          
   
                Future<Boolean> f = executorService.submit(new Callable<Boolean>() {
          
   
                    @Override
                    public Boolean call() throws Exception {
          
   
                        System.out.println(i);
                        return true;
                    }
                });
                booleans.add(f);
            }
			//打印信息形成线程阻塞,不然容易出现问题
            for (int i = 0; i < booleans.size(); i++) {
          
   
                System.out.println(tableName + "内层分区循环:" + i + " " + booleans.get(i).get());
            }
        } catch (Exception exx) {
          
   
            exx.printStackTrace();
            System.out.println("程序异常:" + exx.getMessage());
        } finally {
          
   
            if (executorService != null) {
          
   
                executorService.shutdown();
            }
//            JedisPoolUtils.destroy();
        }
经验分享 程序员 微信小程序 职场和发展