test/serviceability/threads/TestFalseDeadLock.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff test/serviceability/threads

test/serviceability/threads/TestFalseDeadLock.java

Print this page
rev 7259 : 8044186: Introduce a reproducible random generator
Reviewed-by: iignatyev
Contributed-by: sergei.kovalev@oracle.com
   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");
   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");
test/serviceability/threads/TestFalseDeadLock.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File