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