1/*
2 * Copyright (c) 2011, 2016, 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/*
25 * @test
26 * @bug     6977677 8004928
27 * @summary Deadlock between LogManager.<clinit> and Logger.getLogger()
28 * @author  Daniel D. Daugherty
29 * @modules java.base/sun.util.logging
30 *          java.logging
31 * @compile -XDignore.symbol.file LoggingDeadlock4.java
32 * @run main/othervm/timeout=15 LoggingDeadlock4
33 */
34
35import java.util.concurrent.CountDownLatch;
36import java.util.logging.LogManager;
37import java.util.logging.Logger;
38
39public class LoggingDeadlock4 {
40    private static CountDownLatch barrier      = new CountDownLatch(1);
41    private static CountDownLatch lmIsRunning  = new CountDownLatch(1);
42    private static CountDownLatch logIsRunning = new CountDownLatch(1);
43
44    // Create a sun.util.logging.PlatformLogger$JavaLogger object
45    // that has to be redirected when the LogManager class
46    // is initialized. This can cause a deadlock between
47    // LogManager.<clinit> and Logger.getLogger().
48    private static final sun.util.logging.PlatformLogger log =
49        sun.util.logging.PlatformLogger.getLogger("java.util.logging");
50
51    public static void main(String[] args) {
52        System.out.println("main: LoggingDeadlock4 is starting.");
53
54        Thread lmThread = new Thread("LogManagerThread") {
55            public void run() {
56                // let main know LogManagerThread is running
57                lmIsRunning.countDown();
58
59                System.out.println(Thread.currentThread().getName()
60                    + ": is running.");
61
62                try {
63                    barrier.await();  // wait for race to start
64                } catch (InterruptedException e) {
65                }
66
67                LogManager.getLogManager();
68            }
69        };
70        lmThread.start();
71
72        Thread logThread = new Thread("LoggerThread") {
73            public void run() {
74                // let main know LoggerThread is running
75                logIsRunning.countDown();
76
77                System.out.println(Thread.currentThread().getName()
78                    + ": is running.");
79
80                try {
81                    barrier.await();  // wait for race to start
82                } catch (InterruptedException e) {
83                }
84
85                Logger.getLogger("foo logger");
86            }
87        };
88        logThread.start();
89
90        try {
91            // wait for LogManagerThread and LoggerThread to get going
92            lmIsRunning.await();
93            logIsRunning.await();
94        } catch (InterruptedException e) {
95        }
96
97        barrier.countDown();  // start the race
98
99        try {
100            lmThread.join();
101            logThread.join();
102        } catch (InterruptedException ie) {
103        }
104
105        System.out.println("main: LoggingDeadlock4 is done.");
106    }
107}
108