Concurrency and Threads
LockedLesson 25 of 60 · Concurrency · Challenging
Finish these first:Exceptions →Lists and Sets →Or pass the tests below to unlock it now.
The big idea
Use ExecutorService over raw threads. Protect shared state with synchronized, locks, or concurrent/atomic classes.
Think of it like this: Think of concurrency like multiple chefs sharing one kitchen: coordinating who uses the stove prevents two chefs from overwriting the same dish at the same moment.
Read the lesson
Use ExecutorService over raw threads. Protect shared state with synchronized, locks, or concurrent/atomic classes. Beware races and deadlocks. Virtual threads (Java 21+) make blocking I/O cheap.
try (var pool = Executors.newFixedThreadPool(4)) {
Future<Integer> f = pool.submit(() -> 21 * 2);
int v = f.get();
}
AtomicInteger n = new AtomicInteger();
Quick check
Before writing code: What should `Solution.parallelSum(new int[]{1,2,3,4})` return when your implementation of Concurrency and Threads is working?
Your challenge
Write static int parallelSum(int[] xs) that sums using multiple threads safely.
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.concurrency-asyncio (40% overlap). Python asyncio is cooperative; Java uses threads/executors (virtual threads for I/O).
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Concurrency and Threads.
Learn this first
- ExceptionsNot yet
- Lists and SetsNot yet
Opens up next
This is the last lesson in this branch.