Unit 2 home
Class 23 of 60 Part I · Orange UI24PC320CS
Placeholder · not written yet

Two students book the last seat at the same instant

Both threads check the seat count, both see one left, both book it. The cinema has now sold seat 14 twice. This session shows that happening on a real booking screen, then fixes it with one keyword.

Class 23 Part I · C21–C24 1 PYQ · P2·Q3 Feeds Lab 5

This session has not been authored yet

You have reached a real page, but an empty one. The deck for Class 23 is planned in full in the course knowledge document and is queued for authoring — nothing on this page is finished teaching content.

The roster underneath is the actual plan for this session, so you can see exactly what will land here.

What this class will cover

  1. Class-23 openerThe hook: the same seat, sold twice, by a program with no bugs in it.
  2. What a race condition isShared state plus unlucky timing — and why it is invisible most of the time you run it.
  3. The seat-booking screensA real movie-ticket seat map: two bookings click the same seat, and the counter goes wrong on screen.
  4. Why check-then-act breaksThe gap between reading the count and updating it, drawn out instruction by instruction.
  5. synchronized methodsOne lock, one thread at a time — bookTicket() before and after, side by side.
  6. synchronized blocksLocking the smallest thing that needs locking, instead of the whole method.
  7. volatileVisibility, not atomicity — the distinction that the exam and interviews both probe.
  8. PYQ · P2·Q3Why synchronization matters in multithreading, answered in full with the seat-booking program.
  9. Activities + close cardBreak it, watch it fail, fix it — then the runway into wait() and notify().

Seat-locking, shown on screen

This session and Lab 5 both use real movie-ticket seat-booking screens — you click a seat, watch the lock get taken, and see what happens when a second booking clicks the same seat a millisecond later. The race is demonstrated visually before a single line of theory is defended.