提交 c7ff2fce 编写于 作者: S smarks

8056313: TEST_BUG: java/util/Timer/NameConstructors.java fails intermittently

Reviewed-by: lancea, rriggs
上级 f2328e79
/* /*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,37 +23,42 @@ ...@@ -23,37 +23,42 @@
/* /*
* @test * @test
* @bug 4279061 * @bug 4279061 8056313
* @summary Basic test for constructors with thread name * @summary Basic test for constructors with thread name
*/ */
import java.util.*; import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedTransferQueue;
public class NameConstructors { public class NameConstructors {
private static final String NAME1 = "Norm D. Plume"; private static final String NAME1 = "Norm D. Plume";
private static final String NAME2 = "Ann Onymous"; private static final String NAME2 = "Ann Onymous";
public static void main (String[] args) throws Exception { public static void main (String[] args) throws InterruptedException {
Random rnd = new Random();
test(new Timer(NAME1), NAME1); test(new Timer(NAME1), NAME1);
test(new Timer(NAME2, true), NAME2); test(new Timer(NAME2, true), NAME2);
} }
private static boolean done, passed; public static void test(Timer timer, String expected) throws InterruptedException {
try {
public static void test(Timer timer, final String name) throws Exception { LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
done = passed = false;
TimerTask task = new TimerTask() { TimerTask task = new TimerTask() {
public void run() { public void run() {
passed = Thread.currentThread().getName().equals(name); queue.put(Thread.currentThread().getName());
done = true;
} }
}; };
timer.schedule(task, 0); // Immediate
Thread.sleep(500); timer.schedule(task, 0L); // immediately
if (!(done && passed)) String actual = queue.take();
throw new RuntimeException(done + " : " + passed);
if (!expected.equals(actual)) {
throw new AssertionError(
String.format("expected='%s', actual='%s'", expected, actual));
}
} finally {
timer.cancel(); timer.cancel();
} }
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册