Introduction
Concurrency bugs hide in timing. This project avoids that trap twice over: it implements transfers with two deadlock-free locking strategies, and it designs its experiment so that the correct final state is known in advance and can be checked exactly, however the threads interleave.
Problem statement
Objectives
- Prevent deadlock with lock ordering and with tryLock back-off
- Design concurrent tests whose correct answer is deterministic
- Coordinate threads with ExecutorService, CountDownLatch and Futures
- Count events safely with AtomicLong
Features
- Two interchangeable thread-safe strategies (abstract class)
- Reproducible, seeded workloads
- Invariant checks: conservation, non-negativity, sequential equivalence
- Identical output on every run
Concepts demonstrated
- Race conditions
- Deadlock avoidance
- Lock ordering
- Back-off
- Deterministic testing of concurrent code
System architecture
Bank is abstract with two nested implementations. OrderedLocks gives every account its own monitor and always locks the lower index first, so a cycle of waiting threads can't form. TryLocks locks in the caller's order (which would deadlock with plain lock()), but if the second lock isn't free within 50 µs it releases the first, yields and retries. Main generates the transfers from the seed, computes the expected balances sequentially, runs each bank on a fixed pool with a start latch, and compares the results. Each account starts with (total transfers × maximum amount), so a transfer can never be rejected.
Folder structure
Complete source code
src/Bank.java
What each file does
- src/Bank.java
- The abstract bank and its two locking strategies.
- src/Main.java
- Workload generation, the concurrent run with a start latch, and the invariant checks.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
6 8 2000 2026
Testing
- Run with
tests/sample-input.txt(6 accounts, 8 threads, 2,000 transfers each, seed 2026) and compare withtests/expected-output.txt. - Both strategies complete all 16,000 transfers, conserve the ₹4,800,000 total and match the sequential replay.
- Run the test several times: the output never changes, although the threads interleave differently each time.
- Pass
--verboseto see the number of tryLock retries, which does vary between runs.
Common errors
The program hangs
Two threads are each holding one lock and waiting for the other: order the locks globally or use tryLock with back-off.
The total changes between runs
Some update happens outside the locks; every read and write of shared balances must hold the account's lock.
Possible improvements
- Add an unsafe implementation to demonstrate lost updates
- Measure throughput of each strategy with JMH
- Use StampedLock for balance reads
Additional challenges
- 1Allow transfers to fail for insufficient funds and verify conservation still holds.
- 2Add a third strategy using a single global lock and compare.
- 3Detect deadlocks with ThreadMXBean when ordering is removed.
Built and tested it yourself? Mark it to update your dashboard.