2009-05-30 58 views
2

我正在開發一個隊列模擬,使用擺動計時器在一段時間後退出對象。間隔取決於隊列中的下一個對象,從中獲取一個整數,並設置相應定時器的延遲。在運行期間修改擺輪定時器的延遲

下面是該程序的相關代碼段(注:_SECONDS_PER_ITEM是在其他地方規定2000常數):

// stop the timer 
qTimer[q].stop(); 

// peek at how many items the customer has, and set the delay. 
qTimer[q].setDelay(customerQueue[q].peek().getItems()*_SECONDS_PER_ITEM); 

// the next time around, this method will see the flag, and dequeue the customer. 
working[q] = true; 

// denote that the customer is active on the UI. 
lblCustomer[q][0].setBorder(new LineBorder(Color.RED, 2)); 

// start the timer. 
qTimer[q].start(); 

我的問題是,每一個客戶,無論多少他們的項目,被處理在一秒鐘內。

是否有其他方法或技術我應該用來設置延遲?

回答

3

看來,當stop() ing定時器,用於觸發下一個事件的延遲是初始延遲。因此,在上例中使用的正確方法是setInitialDelay()

{ 
// stop the timer 
qTimer[q].stop(); 

// peek at how many items the customer has, and set the delay. 
qTimer[q].setInitialDelay(customerQueue[q].peek().getItems()*_SECONDS_PER_ITEM); 

// the next time around, this method will see the flag, and dequeue the customer. 
working[q] = true; 

// denote that the customer is active on the UI. 
lblCustomer[q][0].setBorder(new LineBorder(Color.RED, 2)); 

// start the timer. 
qTimer[q].start(); 

}