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(); }
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究