使用jdk的java.util.concurrent.Executors类,实例如下,自行领悟其中的道理,自己跑下就知道了
// 开启线程 ExecutorService cacheThreadPool = Executors.newCachedThreadPool(); Callable<Boolean> clientAction = new Callable<Boolean>() { @Override public Boolean call() throws Exception { // 请求客户端 RestTemplate restTemplate = new RestTemplate(); final String uri = url + "/generateCodeProvider/gen/cap/cap.cs"; restTemplate.postForObject(uri, postParam, Boolean.class); return true; } }; try { cacheThreadPool.submit(clientAction); } catch (Exception e) { LOGGER.error("执行clientAction失败:" + e.getMessage(), e); } finally { cacheThreadPool.shutdown(); }