jdk

Форк
0
/
TestFalseDeadLock.java 
104 строки · 3.4 Кб
1
/*
2
 * Copyright (c) 2013, 2020, 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 jdk.test.lib.Utils;
25
import java.lang.management.ManagementFactory;
26
import java.lang.management.ThreadMXBean;
27
import java.util.Random;
28

29
/*
30
 * @test
31
 * @key randomness
32
 * @bug 8016304
33
 * @summary Make sure no deadlock is reported for this program which has no deadlocks.
34
 * @modules java.base/jdk.internal.misc
35
 * @library /test/lib
36
 * @run main/othervm TestFalseDeadLock
37
 */
38

39
/*
40
 * This test will not provoke the bug every time it is run since the bug is intermittent.
41
 * The test has a fixed running time of 5 seconds.
42
 */
43

44
public class TestFalseDeadLock {
45
    private static ThreadMXBean bean;
46
    private static volatile boolean running = true;
47
    private static volatile boolean found = false;
48

49
    public static void main(String[] args) throws Exception {
50
        bean = ManagementFactory.getThreadMXBean();
51
        Thread[] threads = new Thread[500];
52
        Random random = Utils.getRandomInstance();
53
        for (int i = 0; i < threads.length; i++) {
54
            Test t = new Test(random.nextLong());
55
            threads[i] = new Thread(t);
56
            threads[i].start();
57
        }
58
        try {
59
            Thread.sleep(5000);
60
        } catch (InterruptedException ex) {
61
        }
62
        running = false;
63
        for (Thread t : threads) {
64
            t.join();
65
        }
66
        if (found) {
67
            throw new Exception("Deadlock reported, but there is no deadlock.");
68
        }
69
    }
70

71
    public static class Test implements Runnable {
72
        private final long seed;
73
        public Test(long seed) {
74
            this.seed = seed;
75
        }
76
        public void run() {
77
            Random r = new Random(seed);
78
            while (running) {
79
                try {
80
                    synchronized (this) {
81
                        wait(r.nextInt(1000) + 1);
82
                    }
83
                } catch (InterruptedException ex) {
84
                }
85
                recurse(2000);
86
            }
87
            if (bean.findDeadlockedThreads() != null) {
88
                System.out.println("FOUND!");
89
                found = true;
90
            }
91
        }
92

93
        private void recurse(int i) {
94
            if (!running) {
95
                // It is important for the test to call println here
96
                // since there are locks inside that path.
97
                System.out.println("Hullo");
98
            }
99
            else if (i > 0) {
100
                recurse(i - 1);
101
            }
102
        }
103
    }
104
}
105

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.