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