Callable,Future使用demo

Callable = Runable + return

Future :存储执行的将来才会产生的结果

public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello Callable!";
            }
        };

        ExecutorService service = Executors.newCachedThreadPool();

        Future<String> future = service.submit(callable);//submit方法 异步

        System.out.println(future.get());//future.get()方法 阻塞

        service.shutdown();
    }
发布了48 篇原创文章 · 获赞 1 · 访问量 2823

猜你喜欢

转载自blog.csdn.net/Forest24/article/details/102636721
今日推荐