I found there are two ways (submit and execute) to add a Runnable into a thread pool, what is the difference?
views:
52answers:
3
+3
A:
The difference is that execute
doesn't return a Future
, so you can't wait for the completion of the Runnable
and get any exception it throws using that.
ColinD
2010-10-25 15:30:44
`Future` also allows you to get exceptions thrown by `Runnable`.
axtavt
2010-10-25 15:36:17
@axtavt: True, guess I should mention that too.
ColinD
2010-10-25 15:40:14
+1
A:
Submit appears to be a more generic form of execute. In particular, submit returns a Future object that represents the result of the computation.
AdamH
2010-10-25 15:31:35
+1
A:
The submit(...)
method is executor framework extension introduced in ExecutorService
interface.
It's main difference from execute(Runnable)
is that it can accept Callable<V>
(whereas execute()
accepts only Runnable
) and returns instance of Future<V>
, which you can use later in caller to retrieve result of asynchronous computation (potentially blocking until computation is completed).
Victor Sorokin
2010-10-25 15:36:37