jdk
1/*
2* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*/
23
24import jdk.test.lib.Utils;25import java.lang.management.ManagementFactory;26import java.lang.management.ThreadMXBean;27import java.util.Random;28
29/*
30* @test
31* @key randomness
32* @bug 8016304
33* @summary Make sure no deadlock is reported for this program which has no deadlocks.
34* @modules java.base/jdk.internal.misc
35* @library /test/lib
36* @run main/othervm TestFalseDeadLock
37*/
38
39/*
40* This test will not provoke the bug every time it is run since the bug is intermittent.
41* The test has a fixed running time of 5 seconds.
42*/
43
44public class TestFalseDeadLock {45private static ThreadMXBean bean;46private static volatile boolean running = true;47private static volatile boolean found = false;48
49public static void main(String[] args) throws Exception {50bean = ManagementFactory.getThreadMXBean();51Thread[] threads = new Thread[500];52Random random = Utils.getRandomInstance();53for (int i = 0; i < threads.length; i++) {54Test t = new Test(random.nextLong());55threads[i] = new Thread(t);56threads[i].start();57}58try {59Thread.sleep(5000);60} catch (InterruptedException ex) {61}62running = false;63for (Thread t : threads) {64t.join();65}66if (found) {67throw new Exception("Deadlock reported, but there is no deadlock.");68}69}70
71public static class Test implements Runnable {72private final long seed;73public Test(long seed) {74this.seed = seed;75}76public void run() {77Random r = new Random(seed);78while (running) {79try {80synchronized (this) {81wait(r.nextInt(1000) + 1);82}83} catch (InterruptedException ex) {84}85recurse(2000);86}87if (bean.findDeadlockedThreads() != null) {88System.out.println("FOUND!");89found = true;90}91}92
93private void recurse(int i) {94if (!running) {95// It is important for the test to call println here96// since there are locks inside that path.97System.out.println("Hullo");98}99else if (i > 0) {100recurse(i - 1);101}102}103}104}
105