2017-02-24 68 views
1

我正在嘗試構建一個交通燈系統來控制交界處。我有一個控制器類,將執行交界處的階段,例如,第一階段將使用Thread.sleep將南北和南北交通燈轉爲綠燈一段時間,然後在第二階段相同階段,但東西和西南交通燈。使用執行服務同時執行多個對象的方法

我的問題是我想把每個階段的所有交通燈都集中在一起,這樣南北和南北交通燈就會變成綠色並同時變紅。我試圖使用ExecutedService框架,但它似乎並沒有正常工作,這裏是我的代碼。如果你爲了給我一個有用的答案需要更多的信息,請不要猶豫,問

這是將運行階段連續控制器具有階段爲對象的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

從我理解你的代碼中,只有一個線程將運行一個在執行您的ExecutorService方法(您只能在您的代碼中執行一個Runnable!)。

再次,如果我的理解是正確的,你應該有每個交通燈實現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) { 
        } 

       } 
} 
+0

我實現Runnable接口應該是什麼run()方法執行後?只有你給了什麼?和你的代碼應該在哪個類中? –

+0

你的回答非常明確,但是在第一階段將所有交通信號燈連接在一起,而不會觸及下一階段的交通信號燈? –

+0

我看到你使用了交通燈類來編寫邏輯。不能從階段或控制器? –