views:

135

answers:

1

I'm developing a Queue simulation, using a Swing Timer to dequeue objects after certain amounts of time. The interval is determined by peeking at the next object in the queue, getting an integer from it, and setting the delay of its corresponding timer.

Here's the relevant snippet from the program (Note: _SECONDS_PER_ITEM is a constant defined elsewhere to 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();

The problem I have is that every customer, no matter how many items they have, is processed in one second.

Is there some other method or technique I should be using to set the delay?

+1  A: 

It would seem that when stop()ing a Timer, the delay that is used to fire the next event is the initial delay. Thus, the correct method to use in the above example, is 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();

}
Kindinos