PrepZone Logo
PrepZone

Thread Safety Fundamentals

Shared mutable state, races, and the patterns that fix them.

Why this matters

  • Race conditions — detailed pointwise breakdown in the section below.
  • Immutability — detailed pointwise breakdown in the section below.
  • Confinement — detailed pointwise breakdown in the section below.
  • Synchronised blocks — detailed pointwise breakdown in the section below.
  • Concurrency appears in elevator, booking, cache, and feed problems.
Unsynchronised — update lost
A reads 5
B reads 5Before A writes
A writes 6
B writes 6Expected 7
Synchronised — correct
A acquires the lock
A reads 5, writes 6
A releases, B acquires
B reads 6, writes 7
count++ is read, add, write — three steps. Two threads can read the same value before either writes, so one increment disappears. A lock makes the three steps atomic.

Overview

Shared mutable state, races, and the patterns that fix them. Study each section below in order — every list is interview-ready reference material.

Race conditions

Points to cover

  • Definition — what Race conditions means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Immutability

Points to cover

  • Definition — what Immutability means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Confinement

Points to cover

  • Definition — what Confinement means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Synchronised blocks

Points to cover

  • Definition — what Synchronised blocks means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design
Java
// CampusOS — apply "Race conditions" on paper first, then in code
public final class ThreadSafetyFundamentalsExample {
    // 1. Name entities from the problem statement
    // 2. Draw relationships before writing methods
    // 3. Introduce patterns only when a force appears
}

Quick recall

Everything you need if you only revisit this box.

  1. Race conditions — review this slice before mock interviews.
  2. Immutability — review this slice before mock interviews.
  3. Confinement — review this slice before mock interviews.
  4. Synchronised blocks — review this slice before mock interviews.

Test yourself

Answer these before moving on — recall is what makes it stick.