zoukankan      html  css  js  c++  java
  • java中ExecutorService使用多线程处理业务

    ExecutorService executorService = Executors.newFixedThreadPool(5);  
      
    List<CancelApprovalCallable> callables = new List<>();  
    for(int i=0,len=idsArray.size();i<len;i++){
        String id = idsArray.get(i);
        CancelApprovalCallable callable = new CancelApprovalCallable(id,domain);
        callables.add(callable);
    }
    List<Future<JSONObject>> resultList = new ArrayList<>();
    try{
        resultList = executorService.invokeAll(callables);
    }catch(InterruptedException e){
        log.error("execute concurrent thread error",e);
    }finally{
        if(!executorService.isShutdown() || !executorService.isTerminated()){
            executorService.shutdown();
        }
    }
    
    /**
    *批量获取线程执行结果,循环处理每条结果数据
    */
    for(Future<JSONObject> future : resultList){
        JSONObject resp2 = null;
        try{
            resp2 = future.get();
        }catch(Exception e){
            log.error("execute concurrent thread error",e);
        }
        if(resp2 == null){
            continue;
        }
    }
    class CancelApprovalCallable implements Callable<JSONbject>{
            
            private String id;
            private String domain;
            CancelApprovalCallable(String id,String domain){
                this.id=id;
                this.domain = domain;
            }
            /* (non-Javadoc)
             * @see java.util.concurrent.Callable#call()
             */
            @Override
            public JSObject call() throws Exception {
                
                return null;
            }
            
        }
  • 相关阅读:
    final
    职场语句
    故事
    三个关键字
    关于重读字母去掉的代码
    Java书
    docker私库harbor的搭建
    配置允许匿名用户登录访问vsftpd服务,进行文档的上传下载、文档的新建删除等操作
    docker容器内外相互拷贝数据
    docker
  • 原文地址:https://www.cnblogs.com/nizuimeiabc1/p/11031218.html
Copyright © 2011-2022 走看看