Callable,Future使用demo

Callable = Runable + return

Future: the future will produce the results of the implementation of storage

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

 

Published 48 original articles · won praise 1 · views 2823

Guess you like

Origin blog.csdn.net/Forest24/article/details/102636721