2

Swing Timer を使用して一定時間後にオブジェクトをデキューする Queue シミュレーションを開発しています。間隔は、キュー内の次のオブジェクトを調べ、そこから整数を取得し、対応するタイマーの遅延を設定することによって決定されます。

プログラムからの関連するスニペットは次のとおりです (注:_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();

私が抱えている問題は、アイテムの数に関係なく、すべての顧客が 1 秒で処理されることです。

遅延を設定するために使用する必要がある他の方法または手法はありますか?

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 に答える