Skip to content
Ameen Abozaid
Planned lessonMID-004-L08

Demo: reproducing a race with a stress test

  • Lesson 8 of 9
  • Mid-level
On This Lesson 3 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

prove a race exists before fixing it; prepares MID-004-LAB1.

Draft outline details

Step by step

  1. Write StockCounter with reserve(int) using check-then-act.
  2. Stress test: 100 threads each reserve 1 unit from 1,000, started together with a CountDownLatch; assert final stock is 900.
  3. Run repeatedly; the test fails intermittently (stock above 900, or oversold).
  4. Planned mistake: add volatile to the field and re-run; it still fails. Explain: visibility is not atomicity.
  5. Fix with synchronized; the test passes across many runs.
  6. Note that a passing run is evidence only when the test failed before the fix.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.