Let say you want to compute a result in the background of a program. Usually it's achieved by encapsulate the task inside a Runnable object and run it in a new thread.
If an error occurs during the process you want your program to be notified and handle errors. But unfortunately the run() method of the Runnable interface doesn't allow to throw exceptions.In this post I will demonstrate a way to achieved that based on Callable and Executors.
The Callable Interface is very similar to Runnable, here is an implementation example :
Here is an example :
In that case main thread never done all task before created thread execution finished.
ReplyDelete