test/hotspot/jtreg/serviceability/threads/TestFalseDeadLock.java
author iignatyev
Thu, 02 Aug 2018 14:40:55 -0700
changeset 51287 7b1ddbafa134
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208655: use JTreg skipped status in hotspot tests Reviewed-by: kvn, hseigel, iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     4
 *
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     8
 *
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    13
 * accompanied this code).
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    14
 *
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    18
 *
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    21
 * questions.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    22
 */
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    23
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 30115
diff changeset
    24
import jdk.test.lib.Utils;
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    25
import java.lang.management.ManagementFactory;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    26
import java.lang.management.ThreadMXBean;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    27
import java.util.Random;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    28
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    29
/*
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    30
 * @test
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    31
 * @bug 8016304
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    32
 * @summary Make sure no deadlock is reported for this program which has no deadlocks.
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 30604
diff changeset
    33
 * @modules java.base/jdk.internal.misc
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    34
 * @library /test/lib
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    35
 * @run main/othervm TestFalseDeadLock
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    36
 */
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    37
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    38
/*
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    39
 * This test will not provoke the bug every time it is run since the bug is intermittent.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    40
 * The test has a fixed running time of 5 seconds.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    41
 */
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    42
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    43
public class TestFalseDeadLock {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    44
    private static ThreadMXBean bean;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    45
    private static volatile boolean running = true;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    46
    private static volatile boolean found = false;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    47
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    49
        bean = ManagementFactory.getThreadMXBean();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    50
        Thread[] threads = new Thread[500];
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    51
        for (int i = 0; i < threads.length; i++) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    52
            Test t = new Test();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    53
            threads[i] = new Thread(t);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    54
            threads[i].start();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    55
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    56
        try {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    57
            Thread.sleep(5000);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    58
        } catch (InterruptedException ex) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    59
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    60
        running = false;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    61
        for (Thread t : threads) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    62
            t.join();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    63
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    64
        if (found) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    65
            throw new Exception("Deadlock reported, but there is no deadlock.");
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    66
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    67
    }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    68
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    69
    public static class Test implements Runnable {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    70
        public void run() {
27453
9aeb9b97bef6 8044186: Introduce a reproducible random generator
iignatyev
parents: 18085
diff changeset
    71
            Random r = Utils.getRandomInstance();
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    72
            while (running) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    73
                try {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    74
                    synchronized (this) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    75
                        wait(r.nextInt(1000) + 1);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    76
                    }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    77
                } catch (InterruptedException ex) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    78
                }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    79
                recurse(2000);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    80
            }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    81
            if (bean.findDeadlockedThreads() != null) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    82
                System.out.println("FOUND!");
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    83
                found = true;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    84
            }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    85
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    86
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    87
        private void recurse(int i) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    88
            if (!running) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    89
                // It is important for the test to call println here
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    90
                // since there are locks inside that path.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    91
                System.out.println("Hullo");
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    92
            }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    93
            else if (i > 0) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    94
                recurse(i - 1);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    95
            }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    96
        }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    97
    }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff changeset
    98
}