Java practice in Concurrency in Java
Browse the rooms before signing in. Opening a room requires an account and follows your existing access. 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 room.
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 room.
Split and Sum
The dataset is large. Split it across threads, sum each slice, and combine, the shape of every parallel job.
joining. Free room.
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 room.
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. Account access required.
No Lock Needed
Skip the lock entirely. An AtomicInteger counts to the exact total under contention with no synchronized in sight.
atomic. Account access required.
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. Account access required.
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. Account access required.
Count It Down
A latch holds the gate shut until the count hits zero. Read how many countDowns remain.
latch. Account access required.
Limited Permits
Only so many may enter. A semaphore hands out permits; count how many requests it can grant.
semaphore. Account access required.
Safe Stack
Many threads push at once. A synchronized stack loses nothing; count every item back out.
stack. Account access required.
Through the Buffer
A producer fills, a consumer drains, a bounded buffer in between. Every item makes it across.
guarded. Account access required.
Every Id Unique
Hand out ids from many threads with no duplicates. Guard the read-and-advance and count the distinct ids.
synchronized. Account access required.
Find the Cycle
Deadlock is a cycle of waiting. Build the wait-for graph and detect the loop that freezes everyone.
deadlock. Account access required.
Lock Order
The cure for deadlock is order. Check whether a thread acquires its locks in increasing id order.
ordering. Account access required.
Tally the Stream
A stream of words, many counting threads. A ConcurrentHashMap with atomic merge gets every tally right.
map. Account access required.
Lock-Free Queue
No locks at all. A ConcurrentLinkedQueue takes offers from every thread and gives them all back.
queue. Account access required.
Many to One
Several workers drain one queue and accumulate the result. Poison each worker so the run ends cleanly.
prodcons. Account access required.
The Job System
The finale: a job system that submits a batch, processes it across a pool, and returns the exact aggregate.
system. Account access required.
The Pipeline
Producers feed, workers transform, the queue connects them. Build a sum-of-squares pipeline that loses nothing.
prodcons. Account access required.