Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/serviceability/threads/TestFalseDeadLock.java
32284 views
/*1* Copyright (c) 2013, 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 java.lang.management.ManagementFactory;24import java.lang.management.ThreadMXBean;25import java.util.Random;2627/*28* @test29* @bug 801630430* @summary Make sure no deadlock is reported for this program which has no deadlocks.31* @run main/othervm TestFalseDeadLock32*/3334/*35* This test will not provoke the bug every time it is run since the bug is intermittent.36* The test has a fixed running time of 5 seconds.37*/3839public class TestFalseDeadLock {40private static ThreadMXBean bean;41private static volatile boolean running = true;42private static volatile boolean found = false;4344public static void main(String[] args) throws Exception {45bean = ManagementFactory.getThreadMXBean();46Thread[] threads = new Thread[500];47for (int i = 0; i < threads.length; i++) {48Test t = new Test();49threads[i] = new Thread(t);50threads[i].start();51}52try {53Thread.sleep(5000);54} catch (InterruptedException ex) {55}56running = false;57for (Thread t : threads) {58t.join();59}60if (found) {61throw new Exception("Deadlock reported, but there is no deadlock.");62}63}6465public static class Test implements Runnable {66public void run() {67Random r = new Random();68while (running) {69try {70synchronized (this) {71wait(r.nextInt(1000) + 1);72}73} catch (InterruptedException ex) {74}75recurse(2000);76}77if (bean.findDeadlockedThreads() != null) {78System.out.println("FOUND!");79found = true;80}81}8283private void recurse(int i) {84if (!running) {85// It is important for the test to call println here86// since there are locks inside that path.87System.out.println("Hullo");88}89else if (i > 0) {90recurse(i - 1);91}92}93}94}959697