Reputation: 67
2.I could see that, Using callbale we can get the exception(if occured), using futuretask.get() method. SO in that case, our Main thread should be waiting till the thread is completed!. Will it not destroy the soul purpose of threading? Is it not a drawback of Callable?
Upvotes: 0
Views: 107
Reputation: 9717
The answers:
FutureTask
, you can pass the Callable
to ExecutorService.submit()
.get()
method is designed to be blocking and it's not the problem. The idea is to execute the functionality in background and meanwhile you can execute other code, you can pass the FutureTask
to some other method, etc.Callable.call()
throws Exception
. If you look at the "standard" Runnable
it does not throw any checked exception, so in the case of error handling you have to put the exception handling logic in the run()
just to compile your program.Upvotes: 0
Reputation: 7720
V get() throws InterruptedException,ExecutionException
Waits if necessary for the computation to complete, and then retrieves its result.
Now
Why is that we need FutureTask instance when we make thread using callable?
If you need the result of callable then you should use it
our Main thread should be waiting till the thread is completed!
Yes So until and unless you really need the result you should avoid using it
You can also use overloaded version So that wait will not be infinite
and you get Timeout Exception as soon as wait()
timed out
Upvotes: 1