1 /* 2 * Copyright (c) 2013, 2014, 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 24 import com.oracle.java.testlibrary.Utils; 25 import java.lang.management.ManagementFactory; 26 import java.lang.management.ThreadMXBean; 27 import java.util.Random; 28 29 /* 30 * @test 31 * @bug 8016304 32 * @summary Make sure no deadlock is reported for this program which has no deadlocks. 33 * @library /testlibrary 34 * @ignore 8061157 35 * @build com.oracle.java.testlibrary.* 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 44 public class TestFalseDeadLock { 45 private static ThreadMXBean bean; 46 private static volatile boolean running = true; 47 private static volatile boolean found = false; 48 49 public static void main(String[] args) throws Exception { 50 bean = ManagementFactory.getThreadMXBean(); 51 Thread[] threads = new Thread[500]; 52 for (int i = 0; i < threads.length; i++) { 53 Test t = new Test(); 54 threads[i] = new Thread(t); 55 threads[i].start(); 56 } 57 try { 58 Thread.sleep(5000); 59 } catch (InterruptedException ex) { 60 } 61 running = false; 62 for (Thread t : threads) { 63 t.join(); 64 } 65 if (found) { 66 throw new Exception("Deadlock reported, but there is no deadlock."); 67 } 68 } 69 70 public static class Test implements Runnable { 71 public void run() { 72 Random r = Utils.getRandomInstance(); 73 while (running) { 74 try { 75 synchronized (this) { 76 wait(r.nextInt(1000) + 1); 77 } 78 } catch (InterruptedException ex) { 79 } 80 recurse(2000); 81 } 82 if (bean.findDeadlockedThreads() != null) { 83 System.out.println("FOUND!"); 84 found = true; 85 } 86 } 87 88 private void recurse(int i) { 89 if (!running) { 90 // It is important for the test to call println here 91 // since there are locks inside that path. 92 System.out.println("Hullo"); 93 } 94 else if (i > 0) { 95 recurse(i - 1); 96 } 97 } 98 } 99 }