提交 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 {
LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
public static void test(Timer timer, final String name) throws Exception { TimerTask task = new TimerTask() {
done = passed = false; public void run() {
queue.put(Thread.currentThread().getName());
}
};
TimerTask task = new TimerTask() { timer.schedule(task, 0L); // immediately
public void run() { String actual = queue.take();
passed = Thread.currentThread().getName().equals(name);
done = true; if (!expected.equals(actual)) {
throw new AssertionError(
String.format("expected='%s', actual='%s'", expected, actual));
} }
}; } finally {
timer.schedule(task, 0); // Immediate timer.cancel();
Thread.sleep(500); }
if (!(done && passed))
throw new RuntimeException(done + " : " + passed);
timer.cancel();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册