I have One Callable which I invoked using
FutureTask<Integer> task = new FutureTask<Integer>(new MyCallable(name, type));
pool = Executors.newSingleThreadExecutor();
pool.submit(task);
I want to know Is execution is continue after pool.submit(task)
or It will wait for callable to complete its execution?
In short I just want to know is there any method like thread.join()
for Callable?
The
pool.submit(callable)
method returns aFuture
and will start executing immediately if the threads are available in the pool. To do ajoin
, you can callfuture.get()
which joins with the thread, returning the value returned by thecall()
method. It is important to note thatget()
may throw anExecutionException
if thecall()
method threw.You do not need to wrap your
Callable
in aFutureTask
. The thread-pool does that for you. So your code would be:This is if your
MyCallable
implementsCallable<String>
of course. TheFuture<?>
will match whatever type yourCallable
is.task.get()
(task being aFutureTask
) expects the current thread to wait for the completion of the managed task by the thread pooler.This method ends up returning either a concrete result or throwing the same checked exception (although wrapped into an ExecutionException) that the job thread would throw during its task.