1. Concept library
  2. Java & Spring Boot
  3. Module 4: Concurrency

Concept

Java memory model

What one thread is guaranteed to see of another thread's writes: happens-before, volatile and safe publication.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain happens-before and what it guarantees between threads
  2. Explain what volatile does and what it does not
  3. Spot the check-then-act that volatile cannot make atomic

In practice

A few lines that show Java memory model doing its job.

Java
class Worker implements Runnable {
    private volatile boolean stopped;     // without volatile the loop may never see the change

    public void run() {
        while (!stopped) { doWork(); }
    }
    public void stop() { stopped = true; }
}

The common mistake

Watch for

Using volatile for a compound action like check-then-act. volatile makes a single read or write visible; it does not make "if (!initialised) initialise()" atomic.

What to know first

Your roadmap puts these before Java memory model, unless you already know them.

How it stays learned

Once a session shows you understand Java memory model, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Related

Previous in the module← Threads and thread safety
Next in the moduleVirtual threads →