? Order lD: 1121404. Pap \"eChegg Study! Guided Sc D Lab! 1.pdf M Update on Lab
ID: 3920047 • Letter: #
Question
? Order lD: 1121404. Pap "eChegg Study! Guided Sc D Lab! 1.pdf M Update on Lab 9,Option: ? Online Java Formatter x x × - ? D Not secure mylinux.langara.bc.ca/-, hlei/CPSC1 181/Lab1 1.pdf Lab11.pdf 215 ght turned greern Waiting for road 1 car Carl to clear intersection Use a loop to print a countdown of the waiting period from 10 to 0. Save your code for later use in this question. 1.2. A single road is a simple queue. The first car in the road will call turnGreen and be removed. After waiting a short amount of time (for example, 5 milliseconds), the next car in the road will call turnGreen, until the road is empty. Part of the code for the RoadRunnable class is given below. It outlines the essential functions, and shows how to implement Runnable. If you are confused about extending Runnable, consider examining its documentation at public class RoadRunnable implements Runnable private LinkedList queue; private int number; private TrafficLight light; public RoadRunnable(int roadNumber, Trafficlight alight)[... public void add (String car) f...} public void run()f . . 1.3. Now create a simulator class to run your code. Fill the four roads with 100 cars each. Then, create a thread for each of the four roads and start the four threads. After you have studied the output consider the interaction between the cars, do any of them collide? Why would activity like that occur? 1.4 It is obviously unacceptable for a traffic light to cause collisions so you will need to adjust your code to keep the cars safe. In lab 10, you synchronized the access of a story by using a lock. Use the same mechanism to synchronize access to the turnGreen function in your traffic light class. Once you are satisfied that none of the cars are colliding, submit your three classes. O Type here to search 247 PM 8/1/2018Explanation / Answer
1.1 Program to simulate a simple traffice light at the intersection of two roads
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StopLight extends Thread {
//private boolean suspendRequest;
private final int sleepTime = 5;
private final Queue carQueue;
private final int roadNum;
public StopLight(int num)
{
roadNum = num;
carQueue = new LinkedList();
for (int i = 1; i < 4; i++){
carQueue.add(i);
}
}
@Override
public void run()
{
while (!carQueue.isEmpty())
{
turnGreen(roadNum);
carQueue.remove();
}
}
public void turnGreen(int roadNumber)
{
synchronized (this)
{
System.out.println("Light turned green for Road " + roadNumber);
System.out.println("Waiting for car " + carQueue.element() + " from Road " + roadNumber + " to clear intersection..");
for(int i = 10; i >= 0; i--)
System.out.print(i + " ");
System.out.println(" Car " + carQueue.element() + " from Road " + roadNumber + " has gone through.");
System.out.println("Light is now red for Road" + roadNumber);
System.out.println(" ");
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
Logger.getLogger(StopLight.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void requestSuspended(){
suspendRequest = true;
}
private synchronized void checkSuspended() throws InterruptedException
{
while (suspendRequest)
wait();
}
public synchronized void requestResume()
{
suspendRequest = false;
notify();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.