Java traps · Chapter 16 of 16

A race on a shared set

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Money
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: two tills, one pass, two free chais

    Write reproduce() in Repro.java. Make a FreeChai with a fake log that holds the first redemption inside the log call until a second redemption reaches the log too: on its first call it counts down one CountDownLatch (it has arrived) and waits on a second latch, at most 200 ms; on its second call it counts down that second latch. Start one thread that redeems pass CP-20931, wait until it is inside the log, start a second thread that redeems the same pass, join both with a timeout, and return how many redemptions returned true.

    The check runs your reproduce() twice: beside the original FreeChai.java it must return 2 (both tills gave the free chai), and beside a fixed FreeChai.java it must return 1. So it must really race two threads on one pass, with the latches deciding the order: no sleeps. Count each thread's latch down in a finally too, so nothing waits for a thread that never reached the log. No real waiting beyond that 200 ms and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    FreeChai.java: one free chai per pass, whatever the timing

    Fix redeem(String passId) in FreeChai.java. However the tills' threads interleave: for a pass not yet used today it calls the log once with free chai for pass <id> and returns true; for a pass already used it returns false and logs nothing. Each pass gets at most one free chai, and different passes never stop each other getting theirs.

    Keep new FreeChai(Consumer<String> log) and the log line, called once for each free chai given. The check uses its own fake log, which holds the first redemption inside the log call until a second redemption has also got past the check (or 200 ms), so the race happens every run; it also redeems different passes at once, the same pass several times in a row and from eight threads, and joins every thread with a timeout, so a lock that is never released fails. A set that is safe for threads does not help on its own: both threads still pass the check before either adds the pass.

  3. Task 3

    FreeChaiTest.java: a test that would have caught it

    Write a JUnit test class, FreeChaiTest.java, that would have caught this race before it reached the counters. The check runs your file three times: beside the original FreeChai.java, where at least one test must fail; beside the fixed FreeChai.java, where every test must pass; and beside a wrong fix that only swaps the HashSet for a concurrent set, where at least one test must fail.

    So force the race the way your repro does, with a fake log that holds the first redemption until the second reaches the log too (at most 200 ms), and assert in the test itself, after joining the threads, that exactly one redemption of the same pass returned true. Join every thread with a timeout. Two threads started without that fake collide only by luck, so such a test may pass the bug one run and not the next. No other real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: two tills, one pass, two free chais
  • FreeChai.java: one free chai per pass, whatever the timing
  • FreeChaiTest.java: a test that would have caught it

More in Money

Build "A race on a shared set" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.