Skip to content

henry-hai/Collision_Avoidance_System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Real-time Java simulation: each car runs on its own thread; a monitor-style TrafficController serializes access to a single-lane bridge using synchronized, wait, and notifyAll, with a Swing UI and EDT-safe updates.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages