Skip to content
Ameen Abozaid
Planned lessonMID-003-L03

Executors and futures

  • Lesson 3 of 7
  • Mid-level
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

  • ExecutorService separates submitting a task from how it runs.
  • Runnable returns nothing; Callable<V> returns a value and may throw.
  • submit returns a Future<V>; get() blocks, optionally with a timeout.
  • ExecutionException wraps the task's exception; the cause is inside.
  • invokeAll runs a batch and returns when all finish.
Draft outline details

Demonstration

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.