test/java/util/Timer/Args.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -21,12 +21,13 @@
* questions.
*/
/*
* @test
- * @bug 6571655 6571881 6574585 6571297
+ * @bug 6571655 6571881 6574585 6571297 8004807
* @summary Test various args to task scheduling methods
+ * @run main/othervm Args
*/
import java.util.*;
import java.util.concurrent.*;
@@ -47,14 +48,20 @@
t.scheduleAtFixedRate(task, d, period);
THROWS(IllegalStateException.class,
new F(){void f(){ t.scheduleAtFixedRate(task, d, period); }});
}
- TimerTask counter(final CountDownLatch latch) {
- return new TimerTask() { public void run() {
- check(latch.getCount() > 0);
+ TimerTask counter(final CountDownLatch latch, final Timer timer, final boolean repeatable) {
+ return new TimerTask() {
+ private boolean executed = false;
+ public void run() {
+ // check if the unrepeatable task is executed repeatedly.
+ check(!executed ? executed = true : repeatable);
latch.countDown();
+ if (latch.getCount() == 0) {
+ timer.cancel();
+ }
}};
}
void test(String[] args) throws Throwable {
final Timer t = new Timer();
@@ -90,28 +97,24 @@
new F(){void f(){ t.scheduleAtFixedRate(null, 42, 42); }},
new F(){void f(){ t.scheduleAtFixedRate(x, (Date)null, 42); }}
);
- final CountDownLatch y1 = new CountDownLatch(1);
- final CountDownLatch y2 = new CountDownLatch(1);
- final CountDownLatch y3 = new CountDownLatch(11);
+ final CountDownLatch y = new CountDownLatch(13);
final long start = System.currentTimeMillis();
final Date past = new Date(start - 10500);
- schedule( t, counter(y1), past);
- schedule( t, counter(y2), past, 1000);
- scheduleAtFixedRate(t, counter(y3), past, 1000);
- y3.await();
- y1.await();
- y2.await();
+ schedule(t, counter(y, t, false), past);
+ schedule(t, counter(y, t, false), past, 1000);
+ scheduleAtFixedRate(t, counter(y, t, true), past, 1000);
+ y.await();
final long elapsed = System.currentTimeMillis() - start;
- System.out.printf("elapsed=%d%n", elapsed);
check(elapsed < 500);
+ System.out.printf("elapsed=%d%n", elapsed);
- t.cancel();
+ t.cancel(); //cancel the timer if it hasn't been cancelled by task
THROWS(IllegalStateException.class,
new F(){void f(){ t.schedule(x, 42); }},
new F(){void f(){ t.schedule(x, past); }},
new F(){void f(){ t.schedule(x, 42, 42); }},