Eleven threads. One molecule.
Making Propane
A synchronization exercise that coordinates eight hydrogen threads and three carbon threads to form propane.
- Java
- Semaphores
- Reusable barriers
Private source · project overview
Use chemistry to ask a systems question
Each atom is a thread. A molecule requires exactly three carbons and eight hydrogens, so a thread must wait until the right companions are available. Semaphores control access and separate queues coordinate the two atom types.
The harder part is reuse
The shared object owns an eleven-atom reusable barrier, but its calls are commented out in both atom threads. The barrier implementation also has counter and permit issues, and Carbon’s queue-release condition only checks the hydrogen count. A successful sample does not establish correctness across schedules. The diagram preserves those boundaries instead of presenting the intended design as verified behaviour.
C C C ─────┐
├── rendezvous ──► C₃H₈
H H H H ───┤
H H H H ───┘Atom threads rendezvous through shared permits.
- 01 / entry point
RunSimulation
Create shared Propane
Start H and C threads
- start → 2. Hydrogen extends Thread
- start → 3. Carbon extends Thread
- 02 / atom
Hydrogen extends Thread
Increment H under mutex
Acquire hydrogensQ → bond
- shared reference → 4. Propane
- 03 / atom
Carbon extends Thread
Increment C under mutex
Acquire carbonQ → bond
- shared reference → 4. Propane
- 04 / shared object
Propane
H / C counters · mutex
hydrogensQ / carbonQ
- release queues → bond → 5. bond(atom)
- owns; calls inactive → 6. BarrierReusable(11)
- 05 / operation
bond(atom)
Print bonding event
Reset counts if H=8 and C=3
- 06 / declared / inactive
BarrierReusable(11)
phase1() / phase2()
b_wait calls commented out
- 1RunSimulation Hydrogen extends Threadstart
- 2RunSimulation Carbon extends Threadstart
- 3Hydrogen extends Thread Propaneshared reference
- 4Carbon extends Thread Propaneshared reference
- 5Propane bond(atom)release queues → bond
- 6Propane BarrierReusable(11)owns; calls inactive
Read from the implementation
RunSimulation.javaHydrogen.java / Carbon.javaPropane.javaBarrierReusable.java