我正在开发一个队列模拟,使用 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();
我遇到的问题是,每个客户,无论他们有多少商品,都会在一秒钟内得到处理。
我应该使用其他方法或技术来设置延迟吗?
最佳答案
看起来,当 stop()
定时器时,用于触发下一个事件的延迟是初始延迟。因此,在上面的示例中使用的正确方法是 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();
}
关于java - 在运行时修改 Swing 计时器的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/930798/