A real-time concurrent simulation in Java where each car operates on an independent thread, simulating high-concurrency environments with parallel execution. Red and blue cars share a single-lane bridge; a TrafficController monitor serializes access to prevent race conditions and collisions.
Multithreading · Thread Synchronization · Swing · Monitor Pattern
- Monitor-based synchronization — A
TrafficControlleracts as a monitor usingsynchronized,wait(), andnotifyAll()to serialize access to the shared bridge. Cars from one direction wait while the opposite direction is crossing; when the bridge clears, waiting threads are woken. - Concurrent car actors — Each car runs as a
Runnableon its own thread. Hundreds of car threads can run in parallel while the controller enforces mutual exclusion at the bridge. - EDT-safe UI — Long-running car logic runs off the Swing Event Dispatch Thread.
SwingUtilities.invokeLateris used when mutating UI state (e.g. adding cars), and a dedicated repaint thread keeps the GUI responsive without blocking the EDT. - Deadlock avoidance — Clear enter/leave protocols and single-monitor design ensure the system remains collision-free and avoids deadlock even under heavy concurrent load.
Requirements: Java JDK (e.g. 8+)
cd Collision_Avoidance_System
javac *.java
java MainUse Add Left and Add Right to spawn red and blue cars. They cross the bridge concurrently; the TrafficController ensures no collisions.
| File | Role |
|---|---|
Main.java |
Entry point; launches UI and background repaint thread |
CarWindow.java |
Swing frame with Add Left / Add Right buttons |
CarWorld.java |
Panel that spawns car threads, shares TrafficController, uses invokeLater for UI updates |
Car.java |
Car actor (Runnable); calls controller enter/leave around bridge segment |
TrafficController.java |
Monitor: enterLeft/enterRight, leaveLeft/leaveRight; wait/notifyAll for coordination |
image/ |
Assets: redcar.gif, bluecar.gif, bridge1.gif |