author | alanb |
Mon, 26 Aug 2013 10:01:27 +0100 | |
changeset 19586 | 08f37e67272c |
parent 18085 | fb6d02a9e1c5 |
child 27453 | 9aeb9b97bef6 |
child 27460 | ca843db0a7e2 |
permissions | -rw-r--r-- |
18085
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
1 |
/* |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
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 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
24 |
import java.lang.management.ManagementFactory; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
25 |
import java.lang.management.ThreadMXBean; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
26 |
import java.util.Random; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
27 |
|
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 |
* @test |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
30 |
* @bug 8016304 |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
31 |
* @summary Make sure no deadlock is reported for this program which has no deadlocks. |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
32 |
* @run main/othervm TestFalseDeadLock |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
33 |
*/ |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
34 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
35 |
/* |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
36 |
* 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
|
37 |
* The test has a fixed running time of 5 seconds. |
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 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
40 |
public class TestFalseDeadLock { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
41 |
private static ThreadMXBean bean; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
42 |
private static volatile boolean running = true; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
43 |
private static volatile boolean found = false; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
44 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
45 |
public static void main(String[] args) throws Exception { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
46 |
bean = ManagementFactory.getThreadMXBean(); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
47 |
Thread[] threads = new Thread[500]; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
48 |
for (int i = 0; i < threads.length; i++) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
49 |
Test t = new Test(); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
50 |
threads[i] = new Thread(t); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
51 |
threads[i].start(); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
52 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
53 |
try { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
54 |
Thread.sleep(5000); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
55 |
} catch (InterruptedException ex) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
56 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
57 |
running = false; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
58 |
for (Thread t : threads) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
59 |
t.join(); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
60 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
61 |
if (found) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
62 |
throw new Exception("Deadlock reported, but there is no deadlock."); |
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 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
65 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
66 |
public static class Test implements Runnable { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
67 |
public void run() { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
68 |
Random r = new Random(); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
69 |
while (running) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
70 |
try { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
71 |
synchronized (this) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
72 |
wait(r.nextInt(1000) + 1); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
73 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
74 |
} catch (InterruptedException ex) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
75 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
76 |
recurse(2000); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
77 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
78 |
if (bean.findDeadlockedThreads() != null) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
79 |
System.out.println("FOUND!"); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
80 |
found = true; |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
81 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
82 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
83 |
|
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
84 |
private void recurse(int i) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
85 |
if (!running) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
86 |
// It is important for the test to call println here |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
87 |
// since there are locks inside that path. |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
88 |
System.out.println("Hullo"); |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
89 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
90 |
else if (i > 0) { |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
91 |
recurse(i - 1); |
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 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
94 |
} |
fb6d02a9e1c5
8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents:
diff
changeset
|
95 |
} |