Difference between executor.submit and executor.execute

£神魔★判官ぃ 2022-06-08 09:58 338阅读 0赞

execute(Runnable) does not return anything.

However, submit(Callable) returns a Future object which allows a way for you to programatically cancel the running thread later as well as get the T that is returned when the Callable completes. See JavaDoc of Future for more details

  1. Future<?> future = executor.submit(longRunningJob);
  2. ...
  3. //long running job is taking too long
  4. future.cancel(true);

Moreover, if future.get() == null and doesn’t throw any exception then Runnable executed successfully

https://stackoverflow.com/questions/18730290/difference-between-executor-submit-and-executor-execute-in-this-code-in-java

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读