سؤال

أقوم بتطوير محاكاة انتظار، باستخدام مؤقت Swing to Dequeue كائنات بعد كميات معينة من الوقت. يتم تحديد الفاصل الزمني عن طريق تطل في الكائن التالي في قائمة الانتظار، والحصول على عدد صحيح منه، ووضع تأخير مؤقته المقابل.

إليك المقتطف ذي الصلة من البرنامج (ملاحظة: _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();

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top