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