2

我正在开发一个队列模拟,使用 Swing Timer 在一定时间后使对象出队。间隔是通过查看队列中的下一个对象,从中获取一个整数,并设置其相应计时器的延迟来确定的。

这是程序中的相关片段(注意:_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();

我遇到的问题是每个客户,无论他们有多少物品,都会在一秒钟内得到处理。

我应该使用其他方法或技术来设置延迟吗?

4

1 回答 1

3

似乎在stop()使用 Timer 时,用于触发下一个事件的延迟是初始延迟。因此,在上述示例中使用的正确方法是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();

}
于 2009-05-30T23:07:44.800 回答