2013-04-16 148 views
-3

我需要每2分鐘向隊列中添加一輛「汽車」,並每隔2分鐘從隊列中刪除一輛「汽車」。然後在可能的情況下,當隊列以相同的時間增加時,將車輛從堆棧移入隊列。每x分鐘執行一段代碼

public static void main(String[] args) { 

    int lp = 10000; 
    int i = 10;  
    int maxQueue = 0; 
    int maxStack = 0; 
    Lstack stack = new Lstack(); 
    Lqueue queue = new Lqueue(); 
    int cr = 1; //cars removed 

    Scanner input = new Scanner(System.in); 

    for (int size = 0; size < 26; size++) { 


     **// arrivals, need to add 2 min wait**   
     if (maxQueue < 6){       
     Car car = new Car(Integer.toString(lp), 'X');    
     queue.insert(car); 
     lp = lp + i; 
     maxQueue++; 
    }  

    else if (maxQueue > 6){ 
     Car car = new Car(Integer.toString(lp), 'X'); 
     stack.push(car); 
     lp = lp + i; 
     maxStack++; 
    } 

    else if((maxQueue > 6) && (maxStack > 6)){ 
     System.out.println("Sorry there isn't enough room"); 
     cr++; 
     System.out.println("New amount to be taken away:" + cr); 
    }  

     **// removals, need to add the wait 5 min** 
     if (maxQueue != 0){    
     for(i = 0; i < cr; i++){ 
      queue.remove(); 
      System.out.println("Car" + lp + "was/were removed"); 
     }    
     maxQueue--; 
     } 

     else if(maxQueue == 0){ 
      cr++; 
     } 
     System.out.println("Before Driveway Moves:"); 
     queue.display(); 
     stack.display(); 

     // empty driveway 

     while((maxQueue < 6) && (maxStack != 0)){ 
      System.out.println("Moving car " + lp + " to the driveway"); 
      queue.insert(stack.pop()); 
     } 
     System.out.println("After Driveway Moves: "); 
     queue.display(); 
     stack.display(); 

    } 
} 
+3

什麼是你的問題? –

+0

我不知道如何在添加和刪除之間添加2分鐘等待和5分鐘等待 – user2284671

+0

您始終可以使用Thread.sleep,儘管當然您不應該在GUI程序中這樣做。 –

回答

1

人們總是可以使用計時器。你希望刪除或插入是相互獨立的嗎?

這是一個定時器的最簡單的例子:

import java.util.Timer; 
import java.util.TimerTask; 

public class TimerEx extends TimerTask { 

    @Override 
    public void run() { 
     System.out.println("I get printed every 2 minutes."); 
    } 

    public static void main(String[] args) { 
     Timer t = new Timer(); 
     t.scheduleAtFixedRate(new TimerEx(), 0, 2*60*1000); 
    } 
} 
+0

時間明智,是的,我想拆卸/插入彼此獨立! – user2284671

+0

對不起,如果這聽起來很愚蠢,但我該如何將其與我現有的代碼整合? – user2284671

+0

@ user2284671你可以做托馬斯先生所說的話。由於涉及線程,請務必保持同步代碼 乾杯 – DarthCoder

0

據我瞭解,你不想只是等待,而是添加/刪除區間車(2,4-後加車, 6 ...分鐘,5,10 ...之後移除,而不是以2分鐘間隔添加所有車輛,然後以5分鐘間隔移除所有車輛)。

最簡單的方法是使用一個線程來爲你計算間隔。你可以嘗試這個課程。

public class Timer implements Runnable 
{ 
    private TimerCallback pCallback; 
    private Thread pThread; 
    private long pInterval; 
    private int pTimerId; 

    private boolean pStopRequested; 

    public interface TimerCallback 
    { 
     public void onTimerTick(int timerId); 
    } 

    public Timer(int id, long interval, TimerCallback callback) 
    { 
     pStopRequested = false; 
     pTimerId = id; 
     pInterval = interval; 
     pCallback = callback; 
     pThread = new Thread(this); 
     pThread.start(); 
    } 

    public void stopTimer() 
    { 
     pStopRequested = true; 
     pThread.interrupt(); 
    } 

    @Override 
    public void run() 
    { 
     while(true) 
     { 
      // wait 
      try 
      { 
       Thread.sleep(pInterval); 
      } 
      catch(InterruptedException e) 
      { 
       // Restore the interrupted status 
       Thread.currentThread().interrupt(); 
      } 

      if(pStopRequested) 
       return; 
      else 
       pCallback.onTimerTick(pTimerId); 
     } 
    } 
} 

這是一個如何使用它的例子。

public class MyCarsQueue 
{ 
    private static final int MINUTE = 1*1000;// 60*1000; 
    private static final int LIMIT = 26; 

    public synchronized void addCar() 
    { 
     // do what you want to do when a new car is added 
    } 

    public synchronized void removeCar() 
    { 
     // do what you want to do when a car is removed 

    } 

    public static void main(String args) 
    { 
     final MyCarsQueue driveway = new MyCarsQueue(); 
     final int[] limit = new int[1]; 

     // create a callback for timers 
     Timer.TimerCallback callback = new Timer.TimerCallback() { 
      @Override 
      public void onTimerTick(int timerId) 
      { 
       if(timerId == 0) driveway.addCar(); 
       if(timerId == 1) driveway.removeCar(); 

       synchronized (limit) { 
        limit[0]++; 
        limit.notify();     
       } 
      } 
     }; 

     // start timers 
     Timer twoMinutesTimer = new Timer(0, 2*MINUTE, callback); 
     Timer fiveMinutesTimer = new Timer(1, 5*MINUTE, callback); 

     // wait until timers have completed 
     try 
     { 
      synchronized (limit) { 
       while(limit[0] < LIMIT) 
       { 
        limit.wait(); 
       } 
      } 
     } 
     catch (InterruptedException e) { 
      // Restore the interrupted status 
      Thread.currentThread().interrupt(); 
     } 

     // finished (the limit has been reached) 
     fiveMinutesTimer.stopTimer(); 
     twoMinutesTimer.stopTimer(); 
    } 
} 

相反定時器可以使用現有的ScheduledExecutorService的

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 

Runnable run = new Runnable() { public void run() { driveway.addCar(); }}; 
ScheduledFuture<?> delayHandle 
    = scheduler.scheduleAtFixedRate(run, 2, 26*2, TimeUnit.MINUTES); 

// wait until delayHandle.isDone(); returns false 
+0

生病了試試吧!感謝一堆,並感謝澄清我的執行間隔不等待! – user2284671