java中ExecutorService使用多线程处理业务

2020-12-13 02:16

阅读:337

标签:pre   cep   div   throws   error   vat   dsa   err   alc   

ExecutorService executorService = Executors.newFixedThreadPool(5);  
  
List callables = new List();  
for(int i=0,len=idsArray.size();i){
    String id = idsArray.get(i);
    CancelApprovalCallable callable = new CancelApprovalCallable(id,domain);
    callables.add(callable);
}
List> 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 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{
        
        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;
        }
        
    }

 

java中ExecutorService使用多线程处理业务

标签:pre   cep   div   throws   error   vat   dsa   err   alc   

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/11031218.html


评论


亲,登录后才可以留言!