Callable,Future使用demo

Callable = Runable + return

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

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

        ExecutorService service = Executors.newCachedThreadPool();

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

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

        service.shutdown();
    }

 

你可能感兴趣的:(Java多线程)