Sie sind auf Seite 1von 3

University of Wisconsin Platteville

University of Applied Sciences Darmstadt


DEPARTMENT OF COMPUTER SCIENCE

Parallel Programming in Java

Prof. Dr. Alois Schtte

Practice 5: Simulation of traffic lights


1. Threads and synchronization

Lets assume, we have a town with a main road around the city. 7 traffic lights are installed to regularize the traffic. 20 cars are simultaneously on this city circle. Simulate this situation using the following framework by extending the program on the yellow marked positions. class TrafficLight { private boolean red; public TrafficLight() { } public synchronized void pass() { // if red, cars have to wait } public synchronized void switchRed() { } public synchronized void switchGreen() { // if green, notify that all cars can start driving } } class Car extends Thread { private TrafficLight[] trafficLights; private int pos; // position of car (before traffic light pos public Car(String name, TrafficLight[] trafficLights) { } private synchronized void goAhead() { pos++; } public synchronized int position() { } public void run() { while(true) { try { sleep((int)(Math.random() * 500)); // driving around for some time } catch(InterruptedException e) {} trafficLights[pos].pass(); // wait before a traffic light goAhead(); // go on driving to next traffic light } }

1/3

University of Wisconsin Platteville

University of Applied Sciences Darmstadt


DEPARTMENT OF COMPUTER SCIENCE

Parallel Programming in Java

Prof. Dr. Alois Schtte

Practice 5: Simulation of traffic lights


public class TrafficLightSimulation { public static void main(String[] args) { TrafficLight[] trafficLights = new TrafficLight[7]; Car[] Cars = new Car[20]; for(int i = 0; i < trafficLights.length; i++) trafficLights[i] = new TrafficLight(); for(int i = 0; i < Cars.length; i++) { Cars[i] = new Car("Car " + i, trafficLights); } while(true) { for(int i = 0; i < trafficLights.length; i = i+2) {
// for each pair of adjacent traffic lights simulate traffic try { // let cars drive around

Thread.sleep(1000); } catch(InterruptedException e) {
// show situation at each traffic light

System.out.println("==================="); for(int j = 0; j < trafficLights.length; j++) { String prefix; if(j == i || j == i+1) prefix = "->"; else prefix = " "; System.out.print(prefix + "in front of light " + j + ":"); for(int k = 0; k < Cars.length; k++) { // cars at traffic light j if(Cars[k].position() == j) System.out.print(" " + k); } System.out.println(); } System.out.println("==================="); try { // let us observe the situation Thread.sleep(5000); } catch(InterruptedException e) {} trafficLights[i].switchGreen(); // switch light i to green if(i+1 < trafficLights.length) trafficLights[i+1].switchGreen(); // switch light i+1 to green try { // wait until swiching to red Thread.sleep((int)(Math.random() * 500)); } catch(InterruptedException e) {} trafficLights[i].switchRed(; // switch light i to red if(i+1 < trafficLights.length) trafficLights[i+1].switchRed(); // switch light i+1 to red

} } // while loop } } // main

2/3

University of Wisconsin Platteville

University of Applied Sciences Darmstadt


DEPARTMENT OF COMPUTER SCIENCE

Parallel Programming in Java

Prof. Dr. Alois Schtte

Practice 5: Simulation of traffic lights


Using this framework could result in a program with the following output: as@hp> java TrafficLightSimulation ====================================================== -> in front of light 0: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -> in front of light 1: in front of light 2: in front of light 3: in front of light 4: in front of light 5: in front of light 6: ====================================================== ====================================================== in front of light 0: in front of light 1: 2 10 11 15 19 -> in front of light 2: 0 1 3 4 5 6 7 8 9 12 13 14 16 17 18 -> in front of light 3: in front of light 4: in front of light 5: in front of light 6: ====================================================== ====================================================== in front of light 0: in front of light 1: 2 10 11 15 19 in front of light 2: in front of light 3: 0 1 3 4 6 7 8 9 12 14 16 17 -> in front of light 4: 5 13 18 -> in front of light 5: in front of light 6: ======================================================

3/3

Das könnte Ihnen auch gefallen