Executors and futures
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
submit tasks and collect results.
Planned topics
ExecutorServiceseparates submitting a task from how it runs.Runnablereturns nothing;Callable<V>returns a value and may throw.submitreturns aFuture<V>;get()blocks, optionally with a timeout.ExecutionExceptionwraps the task's exception; the cause is inside.invokeAllruns a batch and returns when all finish.
Draft outline details
Demonstration
ExecutorService pool = Executors.newFixedThreadPool(4);
Future<BigDecimal> f = pool.submit(() -> supplier.fetchPrice(sku));
BigDecimal price = f.get(2, TimeUnit.SECONDS);Check your understanding
Where does a task's exception surface?
Show answer
In Future.get(), wrapped in ExecutionException.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.