Domanda

sto sviluppando una simulazione coda, utilizzando un timer swing per annullare l'accodamento oggetti dopo una certa quantità di tempo. L'intervallo è determinato dalla sbirciando l'oggetto successivo in coda, ottenendo un intero da esso, e l'impostazione del ritardo del suo timer corrispondente.

Ecco il frammento di rilevante dal programma (Nota: _SECONDS_PER_ITEM è una costante definita altrove per 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();

Il problema che ho è che ogni cliente, indipendentemente dal numero di articoli che hanno, viene elaborato in un secondo.

C'è qualche altro metodo o tecnica dovrei usare per impostare il ritardo?

È stato utile?

Soluzione

Sembrerebbe che quando stop()ing di un timer, il ritardo che viene utilizzato per generare l'evento successivo è il ritardo iniziale. Pertanto, il metodo corretto da utilizzare nell'esempio precedente, è 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();

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top