java · level 3 · unit 6 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a receipt you trade in for the answer.
A Runnable gives nothing back. A does: () -> priceOf(item). Submit one and the pool hands you a Future straight away, like a ticket at a dry cleaner. Later, future.get() trades the ticket for the answer, waiting if the job is still running.
submits a whole list and waits for all of them. Its futures come back in the order of your list, whatever order the threads finished in, so results line up with their jobs every time.
If a job throws, its future keeps the exception. get then throws , with the job's real exception inside as its cause.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
static int wordsIn(String chapter) {
return chapter.split(" ").length;
}
public static void main(String[] args) throws Exception {
List<String> chapters = List.of("It begins here", "The storm arrives at last", "Home");
List<Callable<Integer>> jobs = new ArrayList<>();
for (String chapter : chapters) {
jobs.add(() -> wordsIn(chapter));
}
ExecutorService pool = Executors.newFixedThreadPool(3);
List<Future<Integer>> answers = pool.invokeAll(jobs);
pool.shutdown();
for (Future<Integer> answer : answers) {
System.out.println(answer.get());
}
}
} get can throw two checked exceptions, so this main declares throws Exception to cover both.
guess first
The slow job finishes last. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) throws Exception {
Callable<String> slow = () -> {
Thread.sleep(200);
return "slow";
};
Callable<String> fast = () -> "fast";
ExecutorService pool = Executors.newFixedThreadPool(2);
List<Future<String>> results = pool.invokeAll(List.of(slow, fast));
pool.shutdown();
System.out.println(results.get(0).get() + ", " + results.get(1).get());
}
}
your turn
Your turn. Trade in your tickets.
On line 15, choose any whole number to add up to. On lines 17 and 18, trade each Future for its answer.
output
press run to see what happens
fix it
One class has no marks yet, and everything crashes. Can you fix it?
output
press run to see what happens
getthrowsExecutionException. Its cause,ArithmeticException: / by zero, came from inside a job.- The blue class has no marks, so
averageOfdivides by a size of 0. - At the start of
averageOf, addif (marks.isEmpty()) { return 0; }, then run.
nailed it
every job now hands its answer back. :D
- A
Callablereturns an answer;submitgives aFuturefor it. getwaits for the answer;invokeAllkeeps your list's order.- A crashed job makes
getthrowExecutionException.
new words in your collection
- Callable
- Future
- invokeAll
- ExecutionException
This lesson isn’t saved. A free account remembers every lesson you finish.