jdk实现高性能异步线程开启

使用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();
        }
        


猜你喜欢

转载自blog.csdn.net/melody_susan/article/details/78599326
今日推荐