Runnable 与 Callable的区别:
(1)Callable规定的方法是call(),Runnable规定的方法是run().
(2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的
(3)call方法可以抛出异常,run方法不可以
(4)运行Callable任务可以拿到一个Future对象,Future 表示异步计算的结果。(PS: 特别注意,executorService.submit(Runnable task) 也会返回future, 但是没有future的效果 )
Future接口:
Future接口代表异步计算的结果,通过Future接口提供的方法可以查看异步计算是否执行完成,或者等待执行结果并获取执行结果,同时还可以取消执行。
public interface Future { boolean cancel(boolean mayInterruptIfRunning);//取消任务 boolean isCancelled(); //是否取消了 boolean isDone(); //任务是否完成 V get() throws InterruptedException, ExecutionException; //获取任务执行结果,如果任务还没完成则会阻塞等待直到任务执行完成 V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; //等待一段时间尝试获取执行结果 }
一个使用Runnable的简单例子:
public class RunnableTest { // 创建线程池 private static final ExecutorService executorService = Executors.newFixedThreadPool(5); public static void main(String[] args) { Runnable task = () -> { System.out.println("thread name [" + Thread.currentThread().getName() + "]"); System.out.println("task running ......"); try { Thread.sleep(1000 * 20); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("task end ......"); }; executorService.submit(task); // 关闭线程池 executorService.shutdown(); }
一个使用Callable的简单例子:
public class CallableTest { // 创建线程池 private static final ExecutorService executorService = Executors.newFixedThreadPool(5); public static void main(String[] args) { // 创建task Callable task = () -> { System.out.println("Thread name [" + Thread.currentThread().getName() + "]"); System.out.println("task running ......"); Thread.sleep(1000 * 20); System.out.println("task end ......"); return "call return"; }; Future result = executorService.submit(task); while(!result.isDone()) { System.out.println("waiting task end ......"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } try { System.out.println("thread end, return : " + result.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } // 关闭线程池 executorService.shutdown(); } }