Future和ExecutorService
public void pushDataToRedis(String Code){ Future<String> future = null; List<Future<String>> futureList = new ArrayList<Future<String>>(); ExecutorService executorService = Executors.newFixedThreadPool(20); try { List<St> sts = Lists.newArrayList(); for(St st:sts){ //多线程事件 future= executorService.submit(pushData(Code, AddressList)); futureList.add(future); } // 获取线程处理结果 for (Future<String> f : futureList) { while (true) { if (f.isDone() && !f.isCancelled()) { String res = (String) f.get(); log.info("pushSchDataToRedis info thread " + res); break; } else { Thread.sleep(1000); } } } log.info("end pushSchDataToRedis cityCode:{}",cityCode); }catch (Exception e){ logger.error("pushSchDataToRedis error:" + e.getMessage(), e); } }Callable
private Callable<String> pushData(String Code, List<Address> AddressList) { return new Callable<String>() { @Override public String call() throws Exception { log.info("begin pushData Code:{}", Code); //do sth log.info("end pushData Code:{}", Code); return ""; } }; }