Subj : design help, intersection with threads To : comp.programming.threads From : zaebos Date : Sat Sep 17 2005 03:02 pm Hello, I am trying to learn java threads, and to attempt an exercise from my textbook, hwever I am not sure if any of my proposed solutions are in the right vein... The exercise is to design and implement a program that simulates the behaviour of an intersection used by a number of cars, with four traffic lights and a number of sensors in the road, and a controller that controls the traffic lights based on the sensor information. =B7 The program should include the following timing requirements (all the constants below should be easy to change). o After the traffic lights have changed to a certain colour (green, yellow or red), they must stay that way for a minimum of 2 seconds. o If there are cars waiting in one direction (e.g. north-south), then the lights in the "other" direction (e.g. east-west) must change within approximately 10 seconds, even if there are more cars approaching/entering the intersection from this other direction. o After entering an intersection, cars must spend between 1-3 seconds in the intersection (the exact time must be randomly determined when the car enters the intersection). You do not need to worry about how cars avoid hitting each other in the intersection. =B7 Cars, which must be implemented as threads, should be suspended until the light in the direction from which they have approached has turned green. =B7 Instead of three sensors for each direction, there are only two: the approach and enter sensors are combined into a single sensor. =B7 Cars must enter the intersection in the order of arrival and there must be at least 1 second between successive cars entering the intersection from the same direction (cars from opposite directions may enter the intersection at approximately the same time). Your simulation may define a maximum number of cars, in which case this maximum should be at least 8 and easy to change. The main events under the control of the user should be when in the simulation a car approaches the intersection, from what direction it approaches, and in which direction it leaves. After a car approaches the intersection, a car must enter the intersection when the appropriate light is green and then leave the intersection after between 1-3 seconds (no further user input should be required for this). the time the car stays in for, the length of time it takes the lights to switch colors, and the number of cars must all be definable and easy to change.. =B7 Each car must be implemented as a thread; there may be other threads as well, but this is not necessary. =B7 Threads must suspend by either calling sleep (if they have to wait for a certain amount of time) or by using wait/notify (if they have to wait for a certain condition to hold). There should not be any busy-wait loops in your implementation. =B7 There should be a way to terminate the simulation gracefully (controlled by a user event, not using CTRL-C or the Java System.exit or Thread.stop methods). I am just after some basid design ideas here, Im using java to implement this.., basically i am after the class layout, AFter thinking about this for some time, but finally having to ask for help, I would think that obviously each car as a thread, a trafficlight class, an intersection class which can instantiate trafficlights, and a sensor class, which detects cars arriving and exiting... I am not exactly sure which would be a monitor and which would be a thread... any advice or links to similar problems would be greatly appreciated, Cheers, Jason .