Skip to content

Latest commit

 

History

History
executable file
·
41 lines (28 loc) · 2.3 KB

File metadata and controls

executable file
·
41 lines (28 loc) · 2.3 KB

Collision Avoidance System

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.

Java Multithreading · Thread Synchronization · Swing · Monitor Pattern


Highlights

  • Monitor-based synchronization — A TrafficController acts as a monitor using synchronized, wait(), and notifyAll() 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 Runnable on 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.invokeLater is 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.

How to Run

Requirements: Java JDK (e.g. 8+)

cd Collision_Avoidance_System
javac *.java
java Main

Use Add Left and Add Right to spawn red and blue cars. They cross the bridge concurrently; the TrafficController ensures no collisions.


Project Structure

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