ExecutorService中submit和execute的区别

来源:互联网 发布:四川禄宏微交易 知乎 编辑:程序博客网 时间:2024/05/17 20:29
  1. 接收的参数不一样

  2. submit有返回值,而execute没有

Method submit extends base method Executor.execute by creating and returning a Future that can be used to cancel execution and/or wait for completion.

3、submit方便Exception处理

There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will rethrow this exception, wrapped in an ExecutionException.

submit()能在返回的Future对象调用get()方法的时候再次抛出线程中的异常,而execute()会交由线程的UncaughtExceptionHandler去处理。

1 0
原创粉丝点击