Java practice in Concurrency in Java
Browse the rooms before signing in. All rooms are free with an account. Practice does not issue certificates.
Lock It Down
Eight threads, one counter, one exact total. Guard the increment with synchronized and the number lands right every time.
synchronized. Free with an account.
On a Thread
Your first task at the lab: run a computation on a second thread and bring the answer back. Start it, join it, read the box.
creating. Free with an account.
Split and Sum
The dataset is large. Split it across threads, sum each slice, and combine, the shape of every parallel job.
joining. Free with an account.
Watch the Race
Before you fix a race you must see one. Simulate the read-modify-write steps and watch an update vanish.
race. Free with an account.
Hand It to the Pool
Stop spawning threads by hand. Submit the tasks to an executor, shut it down, and await; every task runs.
executor. Free with an account.
No Lock Needed
Skip the lock entirely. An AtomicInteger counts to the exact total under contention with no synchronized in sight.
atomic. Free with an account.
The Retry Loop
Build the increment yourself out of compare-and-set. Read, compute, CAS; if you lost the race, loop and try again.
cas. Free with an account.
Wait For It
A Callable returns a value, a Future hands it back. get() blocks until it is ready, so the answer is always there.
future. Free with an account.
Count It Down
A latch holds the gate shut until the count hits zero. Read how many countDowns remain.
latch. Free with an account.
Limited Permits
Only so many may enter. A semaphore hands out permits; count how many requests it can grant.
semaphore. Free with an account.
Safe Stack
Many threads push at once. A synchronized stack loses nothing; count every item back out.
stack. Free with an account.
Through the Buffer
A producer fills, a consumer drains, a bounded buffer in between. Every item makes it across.
guarded. Free with an account.
Every Id Unique
Hand out ids from many threads with no duplicates. Guard the read-and-advance and count the distinct ids.
synchronized. Free with an account.
Find the Cycle
Deadlock is a cycle of waiting. Build the wait-for graph and detect the loop that freezes everyone.
deadlock. Free with an account.
Lock Order
The cure for deadlock is order. Check whether a thread acquires its locks in increasing id order.
ordering. Free with an account.
Tally the Stream
A stream of words, many counting threads. A ConcurrentHashMap with atomic merge gets every tally right.
map. Free with an account.
Lock-Free Queue
No locks at all. A ConcurrentLinkedQueue takes offers from every thread and gives them all back.
queue. Free with an account.
Many to One
Several workers drain one queue and accumulate the result. Poison each worker so the run ends cleanly.
prodcons. Free with an account.
The Job System
The finale: a job system that submits a batch, processes it across a pool, and returns the exact aggregate.
system. Free with an account.
The Pipeline
Producers feed, workers transform, the queue connects them. Build a sum-of-squares pipeline that loses nothing.
prodcons. Free with an account.