Core Java · Chapter 38 of 48
Threads
By the end of this chapter you can run work on several threads at once, wait for them, and share a counter, a number or a map between them without losing updates. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Beyond the basics
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
At the 6 am rush a big station runs four counters at once, and they share one stock of bun maska. Yesterday at CSMT the stock went to minus three: two counters both saw "1 left" and both sold it. The token display skipped numbers too. Ravi wants four counters that never sell what is not there.
Every program so far ran on one thread, main. Four counters means four threads changing the same objects at once, and code that is correct on one thread can lose updates on four, silently and only sometimes.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
CounterThreads.java: run a job on a named thread and wait for it
In CounterThreads.java, make runOnWorker(job) run the job on a new thread named tapri-worker, wait until that thread has finished, and return that thread. The job must run on the new thread, never on the thread that called runOnWorker, and the returned thread's state must already be TERMINATED.
For example, a job that records the thread it runs on records the very thread runOnWorker returns, whose name is tapri-worker.
- Task 2
TokenCounter.java: count every token from four threads exactly
In TokenCounter.java, make increment() add one to the count and value() return the count so far, both safe when many threads call them at once: use synchronized. Not one increment may be lost.
For example, four threads that each call increment 10,000 times at the same time leave value() at exactly 40,000; two calls on one thread give 2.
- Task 3
Stock.java: never sell more stock than there is
In Stock.java, make take() take one unit if any is left and return true, or return false when the stock is already empty, without ever letting the stock go below zero, even when many threads call it at once. Do the check and the change in one atomic step on the units field.
For example, a stock of 1,000 buns that four threads try to take from 400 times each gives exactly 1,000 true answers and leaves left() at 0; take() on the empty stock then returns false and left() stays 0.
- Task 4
CounterThreads.java: tally three counters' orders at the same time
In CounterThreads.java, make tally(counters) count how many of each item all the counters sold together. Each inner list is one counter's orders. Tally each counter on its own thread, all running at the same time, into one shared ConcurrentHashMap, and return the map only after every thread has finished. No orders at all gives an empty map.
For example, [[Masala chai, Bun maska], [Masala chai]] gives {Masala chai=2, Bun maska=1}. Three counters of 3,000 orders each give exactly 6,500 Masala chai, 1,500 Cutting chai and 1,000 Bun maska.
- Task 5 · interview question
Interview: CounterThreads.java, predict what run and start do
In CounterThreads.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- Thread: start a job on a new thread, and join to wait for it.
- Race conditions: why count++ loses updates on several threads.
- synchronized: one thread at a time inside a method.
- Atomics and ConcurrentHashMap: a number or a map that many threads can update safely, in one step.
More in Beyond the basics
Build "Threads" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.