void schedule(final Timer t, final TimerTask task, final Date d) {
t.schedule(task, d);
assertThrows
(IllegalStateException.class,
() -> t.schedule(task, d));
}
void schedule(final Timer t, final TimerTask task, final Date d, final long period) {
t.schedule(task, d, period);
assertThrows
(IllegalStateException.class,
() -> t.schedule(task, d, period));
}
void scheduleAtFixedRate(final Timer t, final TimerTask task, final
Date d, finallong period) {
t.scheduleAtFixedRate(task, d, period);
assertThrows
(IllegalStateException.class,
() -> t.scheduleAtFixedRate(task, d, period));
}
TimerTask counter(final CountDownLatch latch) { returnnew TimerTask() { publicvoid run() { if (latch.getCount() == 0)
fail(String.format("Latch counted down too many times: " +
latch));
latch.countDown();
}};
}
void test(String[] args) throws Throwable { final Timer t = new Timer(); try {
test(t);
} finally { // Ensure this test doesn't interfere with subsequent // tests even in case of failure.
t.cancel();
}
// Attempts to schedule tasks on a cancelled Timer result in ISE.
final Date past = new Date(System.currentTimeMillis() - DELAY_MS); final Date future = new Date(System.currentTimeMillis() + DELAY_MS);
assertThrows
(IllegalStateException.class,
() -> t.schedule(nop(), 42),
() -> t.schedule(nop(), 42),
() -> t.schedule(nop(), past),
() -> t.schedule(nop(), 42, 42),
() -> t.schedule(nop(), past, 42),
() -> t.scheduleAtFixedRate(nop(), 42, 42),
() -> t.scheduleAtFixedRate(nop(), past, 42),
() -> t.scheduleAtFixedRate(nop(), future, 42));
}
void test(Timer t) throws Throwable { final TimerTask x = new TimerTask() { publicvoid run() {}};
assertThrows
(IllegalArgumentException.class,
() -> t.schedule(x, -42),
() -> t.schedule(x, new Date(-42)),
// Create local classes for clearer diagnostics in case of failure class OneShotLatch extends CountDownLatch {
OneShotLatch() { super(1); }
} class FixedDelayLatch extends CountDownLatch {
FixedDelayLatch() { super(1); }
} class FixedRateLatch extends CountDownLatch {
FixedRateLatch() { super(11); }
} final CountDownLatch y1 = new OneShotLatch(); final CountDownLatch y2 = new FixedDelayLatch(); final CountDownLatch y3 = new FixedRateLatch();
finallong start = System.currentTimeMillis(); final Date past = new Date(start - (10 * DELAY_MS + DELAY_MS / 2));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.