执行多个对象'方法同时使用Executed Service

时间:2017-02-24 18:08:43

标签: java multithreading traffic

我正在尝试建立一个交通灯系统来控制交叉点。我有一个控制器类,它将执行交叉点的阶段,例如第一阶段将使用Thread.sleep将南北和南北交通灯转为绿灯一段时间,然后在第二阶段相同阶段,但东西和东西交通灯。

我的问题是我想把每个阶段的所有交通信号灯一起转动,这样南北和南北交通信号灯就会变绿并同时变红。我曾尝试使用ExecutedService框架,但它似乎没有正常工作这里是我的代码。如果您需要任何进一步的信息以便给我一个有用的答案,请不要犹豫,

这是控制器类' start方法将连续运行阶段控制器将阶段作为对象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) {
                    }

                }
            }
        });

    }

1 个答案:

答案 0 :(得分:1)

根据我对您的代码的理解,只有一个线程将运行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
}

您也可以查看CountDownLatch类,而不是使用Thread.sleep()。

对于具有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) {
                    }

                }
}