mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
fix intersections starting independently with no coordination
This commit is contained in:
@@ -171,6 +171,29 @@ public class IntersectionProcess {
|
|||||||
System.out.println(" Routing configured.");
|
System.out.println(" Routing configured.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests permission for a traffic light to turn green.
|
||||||
|
* Blocks until permission is granted (no other light is green).
|
||||||
|
*
|
||||||
|
* @param direction The direction requesting green light
|
||||||
|
*/
|
||||||
|
public void requestGreenLight(String direction) {
|
||||||
|
trafficCoordinationLock.lock();
|
||||||
|
currentGreenDirection = direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases the green light permission, allowing another light to turn green.
|
||||||
|
*
|
||||||
|
* @param direction The direction releasing green light
|
||||||
|
*/
|
||||||
|
public void releaseGreenLight(String direction) {
|
||||||
|
if (direction.equals(currentGreenDirection)) {
|
||||||
|
currentGreenDirection = null;
|
||||||
|
trafficCoordinationLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts all traffic light threads.
|
* Starts all traffic light threads.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public class TrafficLightThread implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// Capture the current thread reference
|
|
||||||
this.currentThread = Thread.currentThread();
|
this.currentThread = Thread.currentThread();
|
||||||
this.running = true;
|
this.running = true;
|
||||||
System.out.println("[" + light.getId() + "] Traffic light thread started.");
|
System.out.println("[" + light.getId() + "] Traffic light thread started.");
|
||||||
@@ -37,29 +36,37 @@ public class TrafficLightThread implements Runnable {
|
|||||||
try {
|
try {
|
||||||
while (running && !Thread.currentThread().isInterrupted()) {
|
while (running && !Thread.currentThread().isInterrupted()) {
|
||||||
|
|
||||||
// --- GREEN Phase ---
|
// Request permission to turn green (blocks until granted)
|
||||||
light.changeState(TrafficLightState.GREEN);
|
process.requestGreenLight(light.getDirection());
|
||||||
System.out.println("[" + light.getId() + "] State: GREEN");
|
|
||||||
|
|
||||||
processGreenLightQueue();
|
try {
|
||||||
|
// --- GREEN Phase ---
|
||||||
|
light.changeState(TrafficLightState.GREEN);
|
||||||
|
System.out.println("[" + light.getId() + "] State: GREEN");
|
||||||
|
|
||||||
if (!running || Thread.currentThread().isInterrupted()) break;
|
processGreenLightQueue();
|
||||||
|
|
||||||
// Wait for green duration
|
if (!running || Thread.currentThread().isInterrupted()) break;
|
||||||
Thread.sleep((long) (light.getGreenTime() * 1000));
|
|
||||||
|
|
||||||
if (!running || Thread.currentThread().isInterrupted()) break;
|
// Wait for green duration
|
||||||
|
Thread.sleep((long) (light.getGreenTime() * 1000));
|
||||||
|
|
||||||
// --- RED Phase ---
|
if (!running || Thread.currentThread().isInterrupted()) break;
|
||||||
light.changeState(TrafficLightState.RED);
|
|
||||||
System.out.println("[" + light.getId() + "] State: RED");
|
// --- RED Phase ---
|
||||||
|
light.changeState(TrafficLightState.RED);
|
||||||
|
System.out.println("[" + light.getId() + "] State: RED");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// Always release the green light permission
|
||||||
|
process.releaseGreenLight(light.getDirection());
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for red duration
|
// Wait for red duration
|
||||||
Thread.sleep((long) (light.getRedTime() * 1000));
|
Thread.sleep((long) (light.getRedTime() * 1000));
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
System.out.println("[" + light.getId() + "] Traffic light thread interrupted.");
|
System.out.println("[" + light.getId() + "] Traffic light thread interrupted.");
|
||||||
// Restore interrupt status
|
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
} finally {
|
} finally {
|
||||||
this.running = false;
|
this.running = false;
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
package sd;
|
package sd;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
|
|
||||||
import sd.model.TrafficLight;
|
|
||||||
import sd.model.TrafficLightState;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import sd.model.TrafficLight;
|
||||||
|
import sd.model.TrafficLightState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class to verify traffic light coordination within an intersection.
|
* Test class to verify traffic light coordination within an intersection.
|
||||||
@@ -108,7 +108,7 @@ public class TrafficLightCoordinationTest {
|
|||||||
assertTrue(maxGreenSimultaneously.get() <= 1,
|
assertTrue(maxGreenSimultaneously.get() <= 1,
|
||||||
"At most ONE light should be GREEN at any time. Found: " + maxGreenSimultaneously.get());
|
"At most ONE light should be GREEN at any time. Found: " + maxGreenSimultaneously.get());
|
||||||
|
|
||||||
System.out.println("\n✅ Traffic light coordination working correctly!");
|
System.out.println("\nTraffic light coordination working correctly!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user