These are the notes for Low-Level Design (LLD) episode 46, the first episode of the course's concurrency part. Concurrency means a program dealing with several tasks over the same period. We start with a restaurant kitchen, make our first threads with Thread, Runnable, start and join, watch a thread go through its lifecycle states, and then meet the two classic race conditions: a counter that loses orders (read-modify-write) and the last thali sold twice (check-then-act). The fix is the critical section, guarded by synchronized, which uses every object's intrinsic lock (its monitor), and that lock is reentrant. Every program was actually run with javac Main.java && java -cp . Main (Java 21). Outputs are real, but thread timing is not deterministic: times, the order of lines and the wrong totals change from run to run, so your numbers will differ. What never changes is noted under each output.
1. The real-life idea: one kitchen, several cooks
| In a restaurant | In a computer |
|---|---|
| The restaurant, with its own kitchen, stock and cash | A process: a running program with its own memory, protected from other programs |
| The cooks, sharing one kitchen and one fridge | Threads: paths of work inside one process, sharing its memory |
| One cook juggling dal and roti | Concurrency: several tasks in progress over the same period, by switching |
| Two cooks working at the same instant | Parallelism: tasks running at the very same instant, on different processor cores |
| Process | Thread | |
|---|---|---|
| Memory | Its own | Shared with the other threads of its process |
| Cost to create | High | Low |
| A crash or bad write | Stays inside the process | Can corrupt data other threads use |
| Talking to others | Files, sockets, pipes | Just read and write the same objects (that's the danger) |
Concurrency is about structure (several tasks in progress); parallelism is about execution (at the same instant, which needs several cores). A one-core machine can be concurrent but not parallel.
2. The problem: one thing at a time
Sending an SMS receipt takes about a second (a slow network); Thread.sleep(1000) pretends to be that network. sleep pauses the current thread for the given milliseconds.
public class Main {
static void sendSms(String to) { // a slow job: about 1 second
try { Thread.sleep(1000); } catch (InterruptedException e) { }
System.out.println("SMS sent to " + to + " by " + Thread.currentThread().getName());
}
public static void main(String[] args) {
long t0 = System.currentTimeMillis();
sendSms("Priya"); sendSms("Ravi"); sendSms("Neha"); // one after another
System.out.println("took " + (System.currentTimeMillis() - t0) + " ms");
}
}
Output (real; the time varies slightly):
SMS sent to Priya by main
SMS sent to Ravi by main
SMS sent to Neha by main
took 3032 ms
Every Java program starts with one thread, main. Three SMS one after another take about 3 seconds, mostly spent waiting.
3. Threads: Thread, Runnable, start
Runnable: an interface with one method,run(), holding the code a thread will execute. Written here as a lambda (episode 5).Thread: an object representing one thread.new Thread(runnable, "name").start(): creates the real new thread, which then callsrun(). Creating theThreadobject alone runs nothing.
public class Main {
static void sendSms(String to) { // a slow job: about 1 second
try { Thread.sleep(1000); } catch (InterruptedException e) { }
System.out.println("SMS sent to " + to + " by " + Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException {
long t0 = System.currentTimeMillis();
Thread t1 = new Thread(() -> sendSms("Priya"), "worker-1"); // a Runnable, as a lambda
Thread t2 = new Thread(() -> sendSms("Ravi"), "worker-2");
Thread t3 = new Thread(() -> sendSms("Neha"), "worker-3");
t1.start(); t2.start(); t3.start(); // start: run on a NEW thread
System.out.println("all sent? took " + (System.currentTimeMillis() - t0) + " ms");
}
}
Output (real; the order of the SMS lines varies):
all sent? took 21 ms
SMS sent to Ravi by worker-2
SMS sent to Priya by worker-1
SMS sent to Neha by worker-3
main started the workers and rushed on: "all sent?" printed before any SMS.
4. join: wait for a thread
t.join() makes the current thread wait until t has finished.
public class Main {
static void sendSms(String to) { // a slow job: about 1 second
try { Thread.sleep(1000); } catch (InterruptedException e) { }
System.out.println("SMS sent to " + to + " by " + Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException {
long t0 = System.currentTimeMillis();
Thread t1 = new Thread(() -> sendSms("Priya"), "worker-1"); // a Runnable, as a lambda
Thread t2 = new Thread(() -> sendSms("Ravi"), "worker-2");
Thread t3 = new Thread(() -> sendSms("Neha"), "worker-3");
t1.start(); t2.start(); t3.start(); // start: run on a NEW thread
t1.join(); t2.join(); t3.join(); // join: wait for each to finish
System.out.println("all sent, took " + (System.currentTimeMillis() - t0) + " ms");
}
}
Output (real; the order varies, the time is always about 1 second):
SMS sent to Ravi by worker-2
SMS sent to Priya by worker-1
SMS sent to Neha by worker-3
all sent, took 1046 ms
The three threads waited for the network at the same time. The order changes because the operating system's scheduler decides which thread runs when.
5. The classic mistake: run() instead of start()
public class Main {
static void sendSms(String to) { // a slow job: about 1 second
try { Thread.sleep(1000); } catch (InterruptedException e) { }
System.out.println("SMS sent to " + to + " by " + Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException {
long t0 = System.currentTimeMillis();
Thread t1 = new Thread(() -> sendSms("Priya"), "worker-1"); // a Runnable, as a lambda
Thread t2 = new Thread(() -> sendSms("Ravi"), "worker-2");
Thread t3 = new Thread(() -> sendSms("Neha"), "worker-3");
t1.run(); t2.run(); t3.run(); // run() instead of start()?
t1.join(); t2.join(); t3.join(); // join: wait for each to finish
System.out.println("all sent, took " + (System.currentTimeMillis() - t0) + " ms");
}
}
Output (real):
SMS sent to Priya by main
SMS sent to Ravi by main
SMS sent to Neha by main
all sent, took 3027 ms
Calling run() yourself is an ordinary method call on the current thread: no new thread, back to 3 seconds, all by main.
start() |
run() |
|
|---|---|---|
| New thread? | Yes | No |
| Where the code runs | The new thread | The caller's thread |
| Returns | Immediately | After the work is done |
| Call it twice? | IllegalThreadStateException |
Allowed (it's just a method) |
6. A thread's life: its states
getState() returns a value of the enum java.lang.Thread.State.
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread cook = new Thread(() -> {
long end = System.nanoTime() + 300_000_000;
while (System.nanoTime() < end) { } // chopping: busy for 0.3 s
try { Thread.sleep(500); } catch (InterruptedException e) { } // waiting for the oven: 0.5 s
}, "cook");
System.out.println("before start: " + cook.getState());
cook.start();
Thread.sleep(100); System.out.println("while chopping: " + cook.getState());
Thread.sleep(400); System.out.println("at the oven: " + cook.getState());
cook.join(); System.out.println("after join: " + cook.getState());
cook.start(); // start it again?
}
}
Output (real):
before start: NEW
while chopping: RUNNABLE
at the oven: TIMED_WAITING
after join: TERMINATED
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.start(Thread.java:1525)
at Main.main(Main.java:13)
| State | Meaning | Example |
|---|---|---|
NEW |
Created, not started | Before start() |
RUNNABLE |
Running on a core, or ready and waiting for one | Chopping (busy loop) |
BLOCKED |
Waiting to enter code another thread has locked | Second cashier at synchronized sell |
WAITING |
Waiting with no time limit | join() without a timeout |
TIMED_WAITING |
Waiting with a time limit | sleep(500) |
TERMINATED |
run() has finished, for good |
After join() |
A thread can be started only once; to repeat the job, create a new Thread.
NEW --start()--> RUNNABLE --run() ends--> TERMINATED
| ^
lock taken / join() / sleep() (and back when the pause ends)
v |
BLOCKED / WAITING / TIMED_WAITING
7. Shared mutable state: a counter that loses orders
Shared mutable state: data that more than one thread can change.
class OrderCounter { // shared by both cashiers
private int count = 0;
void add() { // count++ is really three steps:
int c = count; // 1. read
Thread.yield(); // (a switch can happen here; yield: more often)
count = c + 1; // 2. add one, 3. write
}
int get() { return count; }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
OrderCounter counter = new OrderCounter();
Runnable cashier = () -> {
for (int i = 0; i < 100_000; i++) counter.add(); // each cashier rings up 100,000 orders
};
Thread a = new Thread(cashier, "cashier-1"), b = new Thread(cashier, "cashier-2");
a.start(); b.start();
a.join(); b.join();
System.out.println("orders = " + counter.get() + " (expected 200000)");
}
}
Output (real; always far fewer than 200,000):
orders = 100000 (expected 200000)
This is a race condition: the result depends on the timing of threads, which nobody controls. No error, no warning.
count++ is written out as its real steps, with Thread.yield() (a hint to the scheduler to switch threads now) between the read and the write. The scheduler may switch threads at any moment anyway; yield just makes it happen often enough to see on every run. Plain count++ has exactly the same bug, but it shows up less often: in our own tests it lost orders on most runs and gave the right total on a few, which makes it even more dangerous.
Why: read-modify-write
count++ is three steps: read the value, modify it (+1), write it back.
cashier-1: read 5 count = 5
cashier-2: read 5 count = 5 (before cashier-1 wrote!)
cashier-1: 5 + 1 = 6, write 6 count = 6
cashier-2: 5 + 1 = 6, write 6 count = 6 two orders, +1: one LOST
Read-modify-write: an update computed from the old value. When two threads interleave inside it, updates are lost.
8. Check-then-act: the last thali
class Stock {
private int thalis = 1; // only ONE thali left
void sell(String cashier) {
if (thalis > 0) { // check...
pause(100); // (printing the bill takes a moment)
thalis--; // ...then act
System.out.println(cashier + ": SOLD, thalis left = " + thalis);
} else System.out.println(cashier + ": sold out");
}
static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Stock stock = new Stock();
Thread a = new Thread(() -> stock.sell("cashier-1")), b = new Thread(() -> stock.sell("cashier-2"));
a.start(); b.start();
a.join(); b.join();
}
}
Output (real; which line comes first varies, but both always say SOLD and stock goes negative):
cashier-2: SOLD, thalis left = -1
cashier-1: SOLD, thalis left = 0
Check-then-act: deciding on a check (thalis > 0) that another thread can make untrue before you act (thalis--). The same bug books one seat for two people.
9. The fix: critical sections and synchronized
- A critical section is code that reads and changes shared state.
- Rule: only one thread at a time inside it. Then the check and the act happen together, as one atomic step (all of it, with nobody in between).
- In Java, the key to the cash box is
synchronized.
synchronized methods
class Stock {
private int thalis = 1; // only ONE thali left
synchronized void sell(String cashier) { // one cashier at a time
if (thalis > 0) { // check...
pause(100); // (printing the bill takes a moment)
thalis--; // ...then act
System.out.println(cashier + ": SOLD, thalis left = " + thalis);
} else System.out.println(cashier + ": sold out");
}
static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Stock stock = new Stock();
Thread a = new Thread(() -> stock.sell("cashier-1")), b = new Thread(() -> stock.sell("cashier-2"));
a.start(); b.start();
Thread.sleep(50); // peek while one is inside
System.out.println("states: " + a.getState() + ", " + b.getState());
a.join(); b.join();
}
}
Output (real; which cashier wins varies, but exactly one SOLD, and the waiting thread is BLOCKED):
states: TIMED_WAITING, BLOCKED
cashier-1: SOLD, thalis left = 0
cashier-2: sold out
class OrderCounter { // shared by both cashiers
private int count = 0;
synchronized void add() { // one thread at a time, all 3 steps
int c = count; // 1. read
Thread.yield(); // (a switch can happen here; yield: more often)
count = c + 1; // 2. add one, 3. write
}
int get() { return count; }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
OrderCounter counter = new OrderCounter();
Runnable cashier = () -> {
for (int i = 0; i < 100_000; i++) counter.add(); // each cashier rings up 100,000 orders
};
Thread a = new Thread(cashier, "cashier-1"), b = new Thread(cashier, "cashier-2");
a.start(); b.start();
a.join(); b.join();
System.out.println("orders = " + counter.get() + " (expected 200000)");
}
}
Output (real; always exactly 200000):
orders = 200000 (expected 200000)
What synchronized really does: intrinsic locks (monitors)
- Every Java object has a built-in lock: its intrinsic lock, also called its monitor.
- A
synchronizedinstance method locksthis; astatic synchronizedmethod locks the class object (Main.class). - Entering, a thread takes the lock, or waits in
BLOCKEDif another thread holds it. Leaving, even by an exception, it releases the lock automatically. - Locks belong to objects: two
Stockobjects = two locks; only threads using the same object wait for each other.
synchronized void sell() { ... }
// means the same as
void sell() { synchronized (this) { ... } }
synchronized blocks
class Stock {
private final Object lock = new Object(); // a private lock object
private int thalis = 1;
void sell(String cashier) {
String bill = "Bill: 1 thali, Rs 250, " + cashier; // slow work: no lock needed
Stock.pause(100);
boolean sold;
synchronized (lock) { // the critical section only
sold = thalis > 0;
if (sold) thalis--;
}
System.out.println(sold ? bill : cashier + ": sold out");
}
static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Stock stock = new Stock();
Thread a = new Thread(() -> stock.sell("cashier-1")), b = new Thread(() -> stock.sell("cashier-2"));
a.start(); b.start();
a.join(); b.join();
}
}
Output (real; one bill and one "sold out"; which cashier wins varies):
Bill: 1 thali, Rs 250, cashier-2
cashier-1: sold out
- A synchronized block names the object whose lock it uses:
synchronized (lock) { ... }. - Slow work that doesn't touch shared state (preparing the bill) stays outside the lock, so threads wait less.
- A
private final Object lockcan't be grabbed by outside code (anyone can lock a public object, including yourthis). - Rule of thumb: lock as little code as possible, but every access to the shared state, reads included.
10. Reentrancy
class Register {
synchronized void sell(String item) { // takes this object's lock...
System.out.println("selling " + item);
log(item); // ...and calls another synchronized method
}
synchronized void log(String item) { // needs the SAME lock
System.out.println("logged " + item + " by " + Thread.currentThread().getName());
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Register r = new Register();
Thread t = new Thread(() -> r.sell("Masala Dosa"), "cashier-1");
t.start(); t.join();
System.out.println("no deadlock: the lock is reentrant");
}
}
Output (real):
selling Masala Dosa
logged Masala Dosa by cashier-1
no deadlock: the lock is reentrant
Java's intrinsic locks are reentrant: a thread already holding a lock can take it again (the lock counts how many times, and is released when the thread leaves the last level). Without reentrancy, sell() calling log() would wait for itself forever.
Common mistakes
| Mistake | What you see | Why | Fix |
|---|---|---|---|
t.run() instead of t.start() |
No error: everything runs on main, no speed-up |
run() is a normal call |
Always start() |
| Starting a thread twice | Exception in thread "main" java.lang.IllegalThreadStateException |
A thread starts only once | Create a new Thread |
Forgetting join() |
"all sent" printed before the work is done | main doesn't wait |
join() each thread |
count++ from two threads |
No error: fewer than 200000 | Read-modify-write race | synchronized (or atomics, episode 48) |
if (stock > 0) stock--; unlocked |
No error: two SOLD, stock = -1 | Check-then-act race | Check and act under one lock |
| Locking writes but not reads | Stale or half-updated values seen | Reads are accesses too | Same lock for every access (episode 47 explains why) |
| Synchronizing on different objects | Race still happens | Two objects = two locks | One shared lock object |
| Sleeping or doing slow I/O inside a lock | Everything slow, threads BLOCKED |
Lock held too long | Do slow work outside the lock |
Mini project: a thread-safe ticket counter
Ten people (threads) race for three seats. book is a check-then-act, so it is synchronized.
import java.util.*;
class TicketCounter { // a thread-safe seat booking
private final Map<String, String> seats = new HashMap<>(); // seat -> customer
TicketCounter(String... ids) { for (String id : ids) seats.put(id, null); }
synchronized boolean book(String seat, String customer) { // check-then-act, atomically
if (!seats.containsKey(seat) || seats.get(seat) != null) return false;
seats.put(seat, customer);
return true;
}
synchronized int free() { return (int) seats.values().stream().filter(Objects::isNull).count(); }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
TicketCounter counter = new TicketCounter("A1", "A2", "A3");
List<Thread> people = new ArrayList<>();
int[] booked = new int[1];
for (int i = 1; i <= 10; i++) { // 10 people, all want A1 first, then any seat
String name = "person-" + i;
Thread t = new Thread(() -> {
for (String seat : List.of("A1", "A2", "A3"))
if (counter.book(seat, name)) { synchronized (booked) { booked[0]++; } return; }
}, name);
people.add(t); t.start();
}
for (Thread t : people) t.join();
System.out.println("booked: " + booked[0] + ", free seats: " + counter.free());
}
}
Output (real; always 3 booked and 0 free):
booked: 3, free seats: 0
Practice
1. Make a Wallet whose pay(amount) never lets the balance go below zero, even when two threads spend from it at once.
Answer
class Wallet {
private int balance;
Wallet(int balance) { this.balance = balance; }
synchronized boolean pay(int amount) { // check-then-act under one lock
if (balance < amount) return false;
balance -= amount;
return true;
}
synchronized int balance() { return balance; }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Wallet w = new Wallet(1000);
Runnable shopper = () -> { for (int i = 0; i < 100; i++) w.pay(10); }; // tries to spend 1000
Thread a = new Thread(shopper), b = new Thread(shopper);
a.start(); b.start(); a.join(); b.join();
System.out.println("balance = " + w.balance()); // never below 0
}
}
Output (real):
balance = 0
Without synchronized on pay, two threads can both pass the balance < amount check and the balance can go negative.
2. Find the race: if (!map.containsKey(k)) map.put(k, v); run from two threads. Fix it.
Answer
It's check-then-act: both threads can see "not there" and both put, and the second overwrites the first (plain HashMap can even be corrupted by concurrent writes). Put the check and the put under one lock:
import java.util.*;
public class Main {
static final Map<String, Integer> firstOrder = new HashMap<>();
static synchronized void recordFirst(String customer, int orderId) { // check-then-act, fixed
if (!firstOrder.containsKey(customer)) firstOrder.put(customer, orderId);
}
public static void main(String[] args) throws InterruptedException {
Thread a = new Thread(() -> recordFirst("Priya", 101)), b = new Thread(() -> recordFirst("Priya", 102));
a.start(); b.start(); a.join(); b.join();
System.out.println("Priya's first order is recorded once: " + (firstOrder.size() == 1));
}
}
Output (real):
Priya's first order is recorded once: true
(Episode 49 shows ConcurrentHashMap.putIfAbsent, which does this atomically.)
3. Ten threads each sleep 1 second. About how long does the program take with start() + join()? With run()?
Answer
About 1 second with start() + join() (they sleep at the same time); about 10 seconds with run() (all on main, one after another).
4. Two threads call synchronized methods on two different Stock objects. Do they wait for each other?
Answer
No. Each object has its own intrinsic lock; threads only wait when they need the same object's lock.
Quick quiz
Thread t = new Thread(() ->
System.out.println(Thread.currentThread()
.getName()), "chef");
t.run();
What is printed? (a) chef (b) main (c) Thread-0 (d) Nothing
Answer
(b) main. run() is an ordinary method call on the current thread. Only start() creates the thread named chef.
Interview questions
- Process vs thread? A process is a running program with its own memory; threads are paths of execution inside a process that share its memory.
- Concurrency vs parallelism? Concurrency: several tasks in progress over the same period (possibly by switching). Parallelism: tasks executing at the same instant on multiple cores.
start()vsrun()?start()creates a new thread that callsrun(); callingrun()directly executes on the current thread.- Thread states?
NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED. - What is a race condition? A bug where the result depends on the relative timing of threads accessing shared mutable state.
- Why is
count++not thread-safe? It's read-modify-write (three steps); interleaving loses updates. - What is check-then-act? Example? Acting on a check that may be stale:
if (seatFree) book(), lazy initialization,if (!map.containsKey(k)) put. - What does
synchronizeddo? Lets only one thread at a time hold the object's intrinsic lock, making the guarded code atomic with respect to other code using the same lock (and, episode 47, making changes visible). synchronizedmethod vs block? A method locksthis(or the class forstatic) for the whole method; a block locks a chosen object for just the critical section.- What is reentrancy? A thread holding a lock can acquire it again without blocking itself; Java's intrinsic locks are reentrant.
- Is
Runnableor extendingThreadbetter?Runnable(or a lambda): it separates the task from the thread, allows extending another class, and works with thread pools (episode 48).
Glossary
| Term | Meaning, with an example |
|---|---|
| Concurrency | Several tasks in progress over the same period, e.g. one cook juggling dal and roti |
| Parallelism | Tasks running at the very same instant on different cores, e.g. two cooks |
| Process | A running program with its own memory, e.g. the restaurant |
| Thread | One path of execution inside a process, sharing its memory, e.g. a cook |
main thread |
The thread every Java program starts with |
Thread |
The Java class representing a thread: new Thread(task, "worker-1") |
Runnable |
Interface with one method run(): the task a thread executes |
start() |
Creates the new thread, which then calls run() |
run() |
The task's code; calling it directly runs it on the current thread |
join() |
Wait until a thread finishes: t1.join() |
sleep(ms) |
Pause the current thread: Thread.sleep(1000) |
| Scheduler | The part of the operating system deciding which thread runs when |
| Core | One processing unit of a CPU; parallelism needs more than one |
| Thread lifecycle states | NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED |
IllegalThreadStateException |
Thrown when you start a thread a second time |
| Shared mutable state | Data more than one thread can change, e.g. the order count |
| Race condition | A result that depends on thread timing, e.g. lost orders |
| Read-modify-write | An update computed from the old value (count++); races lose updates |
| Check-then-act | Acting on a check another thread may make stale (if (thalis > 0) thalis--) |
| Critical section | Code that reads and changes shared state; one thread at a time |
| Atomic | Happens as one indivisible step; nobody sees it half-done |
synchronized method |
Only one thread at a time runs it on the same object (locks this) |
synchronized block |
synchronized (lock) { ... }: locks a chosen object for a few lines |
| Intrinsic lock | The built-in lock every Java object has |
| Monitor | Another name for an object's intrinsic lock (plus its waiting threads) |
BLOCKED |
The state of a thread waiting to take a lock someone else holds |
| Reentrancy | A thread holding a lock can take it again; Java's intrinsic locks are reentrant |
| Lambda | A short anonymous function: () -> sendSms("Priya") (episode 5) |
Summary
- A process has its own memory; its threads share it. Concurrency = tasks in progress together; parallelism = at the same instant.
start()runs aRunnableon a new thread;join()waits; never callrun()yourself; a thread starts once.- States:
NEW→RUNNABLE↔BLOCKED/WAITING/TIMED_WAITING→TERMINATED. - Shared mutable state + threads = race conditions: lost updates (read-modify-write) and double sales (check-then-act), with no error.
- Guard every critical section with
synchronized, which uses the object's intrinsic lock (monitor); keep locked code small; intrinsic locks are reentrant.
Next episode: Concurrency 2: visibility, volatile and the Java memory model: a stop flag the other thread never sees.