Java Reference
In-Depth Information
Runnable
Callable
It is not generic and the
run()
method does not re-
turn any value.
It is generic and the
call()
method
of a
Callable<V>
instance returns a
value of type
V
.
The
run()
method cannot
throw checked exception.
The
call()
method can throw
checked exception.
• The following code demonstrates how to define a task that will run re-
ports asynchronously:
public class
ReportGeneratorTask implements
Callable<String>{
@Override
public String call()
throws Exception {
//generate report
return "The report was
generated successfully";
}
}
• The following code shows us how to submit a task. We can see that
the
submit()
method of the
ManagedExecutorService
instance
returns an object of type
Future
that will get back the result of the
running task when it becomes available:
Future<String> monitor =
reportGenerator
.submit(new