π¦ Traffic Light Concurrency Problem

Problem Statement:
Simulate a traffic intersection where:
Multiple roads (threads) lead to a single intersection (shared resource).
Only one direction of traffic can pass at a time (mutual exclusion).
Traffic lights (semaphores/locks) control access to avoid accidents (race conditions).
Bonus: Add turn lanes, pedestrian crossings, or emergency vehicle priority.
π‘ Possible Solutions & Approaches
Basic Version (4-Way Intersection)
At a busy four-way intersection, there's no traffic light, but a smart controller is in charge. Each road β North, South, East, and West β has a gatekeeper (a semaphore) that starts locked (red light π«).
The controller runs forever in a loop:
It opens the North and South gates (green light π’) and lets one car from each direction pass.
After 5 seconds, it closes those gates (red light again π΄).
Then it opens the East and West gates for 5 seconds and lets a car from each direction pass.
Repeat foreverβ¦
π Meanwhile, from each direction, cars keep arriving β but they stop at the gate and wait (using acquire()
) until the controller lets them go.
Once the light turns green (via release()
), a car goes through, thanks the controller, and may or may not leave the gate open for the next car (depending on whether the car calls release()
again).
This way, order and safety are maintained at the intersection using semaphores β and no crashes happen!
import java.util.concurrent.Semaphore;
public class TrafficLightBasic {
// Semaphores for each direction (initially 0 = RED)
private static Semaphore north = new Semaphore(0);
private static Semaphore south = new Semaphore(0);
private static Semaphore east = new Semaphore(0);
private static Semaphore west = new Semaphore(0);
public static void main(String[] args) {
// Traffic light controller thread
Thread controller = new Thread(() -> {
while (true) {
try {
System.out.println("\nπ’ NORTH/SOUTH GREEN");
north.release(); // Green for North
south.release(); // Green for South
Thread.sleep(5000); // 5 sec green light
north.acquire(); // Back to red
south.acquire();
System.out.println("\nπ’ EAST/WEST GREEN");
east.release();
west.release();
Thread.sleep(5000);
east.acquire();
west.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
controller.setDaemon(true);
controller.start();
// Simulate cars arriving from different directions
new Thread(() -> car(north, "North Car")).start();
new Thread(() -> car(south, "South Car")).start();
new Thread(() -> car(east, "East Car")).start();
new Thread(() -> car(west, "West Car")).start();
}
private static void car(Semaphore sem, String name) {
while (true) {
try {
sem.acquire(); // Wait for green light
System.out.println(name + " is passing through!");
Thread.sleep(1000); // Time to cross intersection
sem.release(); // Not strictly needed if controller handles it
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
More Solution will be discussed in our course.
Subscribe to my newsletter
Read articles from Subhahu Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
