Callable 和Runnable

1:Callable ,方法调用会有返回值。

    private void callableTest throws ExecutionException, InterruptedException {

        ExecutorService executorService=null;
        Future<Person> future = executorService.submit(new Callable<Person>() {   //executorService 执行 Callable.  Future是一个异步结果的容器。
            @Override
            public Person call() throws Exception {
                return new Person();
            }
        });
        Person person = future.get();  //异步结果,可以用于中断。和FutureTask不同。 FutureTask实现了Runnable,可以是一个任务,可以通过.get获取将来的结果。

    }

2Runnable

    private void runnableTest() throws ExecutionException, InterruptedException {

        ExecutorService executorService=null;
        Future<?> future = executorService.submit(new Runnable() {
            @Override
            public void run() {
                return;
            }
        });
        Object o = future.get();

    }

 

posted @ 2018-07-13 21:49  1367356  阅读(124)  评论(0编辑  收藏  举报