Discard Callable , which is Runnable, which can return a result.
You use it as follows:
You write Callable instead of Runnable, for example:
public class MyCallable implements Callable<Integer> { public Integer call () {
You kick it by sending it to the ExecutionService:
ExecutorService es = Executors.newSingleThreadExecutor (); Future<Integer> task = es.submit(new MyCallable());
You return a FutureTask handle that will contain the result after the task completes:
Integer result = task.get ();
FutureTask provides more methods, such as cancel , isDone and isCancelled , to cancel execution and request status. The get method itself blocks and waits for the task to complete. check out the details of javadoc.
source share