我正在尝试构建一个交通灯系统来控制路口。我有一个 Controller 类,它将执行路口的各个阶段,例如,第一阶段将使用 Thread.sleep 将南北和南北交通灯变为绿灯一段时间,然后在第二阶段中进行相同的操作相位,但适用于东西向和东西向交通灯。
我的问题是我想将每个阶段的所有交通灯一起打开,这样南北和南北交通灯就会同时变绿并变红。我尝试使用 ExecutedService 框架,但它似乎无法正常工作,这是我的代码。如果您需要任何进一步的信息以便给我有用的答案,请随时询问
这是 Controller 类的启动方法,它将连续运行阶段, Controller 将阶段作为对象 LinkedList 的列表。
public void start() throws Exception {
// if there are no phases(cycles) to be executed in sequence
if (phases.isEmpty()) {
throw new Exception("There are no phases been created for the intersection to be controlled");
}
Iterator<Phase> iteratorP = phases.iterator();
//execute phases in sequence
while (iteratorP.hasNext()) {
Phase phase = iteratorP.next();
System.out.println("phase activated: ");
// will activate the phase by turning all the traffic lights in this phase to green light
phase.activate();
}
}
====================================== 这是阶段类。这个类将有一个交通灯列表,它将在一定时间内变成绿色,然后变成红色
protected void activate() {
ExecutorService executorService = Executors.newFixedThreadPool(trafficLights.size());
executorService.execute(new Runnable() {
public void run() {
for (TrafficLight tl : trafficLights) {
tl.nextState();
System.out.println("TrafficLight nextState done");
try {
//PrepareToStopSate = Amber Light
//PrepareToGoState = Amber + Red Lights
if (tl.getCurrentState() instanceof PrepareToStopState ||
tl.getCurrentState() instanceof PrepareToGoState) {
System.out.println("wait 2sec");
//will wait for two seconds then chnage the state to next state GoState or StopState
Thread.currentThread().sleep(pause * 1000);
System.out.println("TL nextState done");
tl.nextState();
} else {
//if the state of the traffic light is either GoState(green) or StopState(red)
//wait for (duration) time then change the state
System.out.println("wait duration for green and red");
Thread.currentThread().sleep(duration * 1000);
}
} catch (Exception e) {
}
}
}
});
}
最佳答案
根据我对您代码的理解,只有一个线程将运行 ExecutorService 的 execute 方法中的线程(您在代码中只执行一个 Runnable!)。
如果我的理解是正确的,您应该让每个 TrafficLight 实现 Runnable,然后编写如下代码:
protected void activate(){
//some code
ExecutorService executorService = Executors.newFixedThreadPool(trafficLights.size());
for (TrafficLight tl: trafficLights){
executor.execute(tl);
}
//some other code
}
此外,您可能不想使用 Thread.sleep(),而是查看 CountDownLatch类。
对于具有 Runnable 的类,这就是您实现逻辑的地方,因此给定您的代码,它看起来像:
public class TrafficLight implement Runnable{
//Some methods
@Override
public void run(){
tl.nextState();
System.out.println("TrafficLight nextState done");
try {
//PrepareToStopSate = Amber Light
//PrepareToGoState = Amber + Red Lights
if (tl.getCurrentState() instanceof PrepareToStopState ||
tl.getCurrentState() instanceof PrepareToGoState) {
System.out.println("wait 2sec");
//will wait for two seconds then chnage the state to next state GoState or StopState
Thread.currentThread().sleep(pause * 1000);
System.out.println("TL nextState done");
tl.nextState();
} else {
//if the state of the traffic light is either GoState(green) or StopState(red)
//wait for (duration) time then change the state
System.out.println("wait duration for green and red");
Thread.currentThread().sleep(duration * 1000);
}
} catch (Exception e) {
}
}
}
关于java - 使用执行服务同时执行多个对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445493/