문제

스윙 타이머를 사용하여 일정 시간 후에 객체를 탈케 타이머를 사용하여 큐 시뮬레이션을 개발하고 있습니다. 간격은 대기열의 다음 객체를 엿보고 정수를 얻고 해당 타이머의 지연을 설정하여 결정됩니다.

다음은 프로그램의 관련 스 니펫입니다 (참고 : _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 초 안에 처리된다는 것입니다.

지연을 설정하기 위해 사용해야 할 다른 방법이나 기술이 있습니까?

도움이 되었습니까?

해결책

언제라도 보일 것입니다 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();

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top