GCGrowCarrer

Concurrency and Threads

Locked

Lesson 25 of 60 · Concurrency · Challenging

Finish these first:Exceptions →Lists and Sets →Or pass the tests below to unlock it now.

1

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.
Key words (tap to learn):
2

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();
3

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?

4

Your challenge

2 sample tests, 1 hidden

Write static int parallelSum(int[] xs) that sums using multiple threads safely.

Stuck? Get a hint (free)
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.

Mastery0%

Learn this first

Opens up next

This is the last lesson in this branch.