java · level 3 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A reuses a few threads for many jobs.
Making a thread for every job is slow, and a thousand threads can sink a server. A thread pool keeps a small fixed team. Executors.newFixedThreadPool(3) gives you an with three threads, and submit(job) drops a job in its queue.
When the jobs are in, call : no new jobs, finish the old ones. Then awaitTermination(5, TimeUnit.SECONDS) waits for them, up to a time limit. Forget shutdown and the pool's threads keep the program alive after main ends.
Submitting after shutdown is refused with RejectedExecutionException, so shut down after the last submit, never inside the loop.
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
List<String> reviews = List.of("Great food", "Slow service but lovely staff", "Would come again", "Too loud");
Map<String, Integer> words = new ConcurrentHashMap<>();
ExecutorService pool = Executors.newFixedThreadPool(3);
for (String review : reviews) {
pool.submit(() -> words.put(review, review.split(" ").length));
}
pool.shutdown();
pool.awaitTermination(5, TimeUnit.SECONDS);
System.out.println(new TreeMap<>(words));
}
} Three threads, four reviews: whichever thread is free first takes the next job from the queue.
guess first
Two washers, seven cars. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws InterruptedException {
AtomicInteger washed = new AtomicInteger();
ExecutorService pool = Executors.newFixedThreadPool(2);
for (int car = 1; car <= 7; car++) {
pool.submit(() -> washed.incrementAndGet());
}
pool.shutdown();
pool.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("Cars washed: " + washed.get());
}
}
your turn
Your turn. Check passwords in parallel.
On line 10, make a pool of 4 threads with newFixedThreadPool. On line 18, tell the pool to take no new jobs.
output
press run to see what happens
fix it
Only one email goes out. Can you fix it?
output
press run to see what happens
- Java throws
RejectedExecutionExceptionon the second email. When does the pool stop taking jobs? shutdownon line 11 runs inside the loop, straight after the first job.- Move
pool.shutdown();below the loop's closing bracket, beforeawaitTermination, then run.
nailed it
your jobs run on a shared team of threads. :D
newFixedThreadPoolmakes a reusable team of threads.submitqueues jobs;shutdowncloses the queue after the last one.awaitTerminationwaits for the queue to finish.
new words in your collection
- thread pool
- ExecutorService
- shutdown
This lesson isn’t saved. A free account remembers every lesson you finish.