1 /*
2  * Copyright (c) 2006, 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 /*
25  * @test
26  * @bug     4994705
27  *
28  * @summary deadlock in LogManager
29  * @author  Serguei Spitsyn / SAP
30  *
31  * @library  /test/lib
32  * @build    LoggingDeadlock
33  * @run  main/timeout=15 LoggingDeadlock
34  * @key randomness
35  */
36 
37 /*
38  *
39  * There can be a deadlock between two class initializations.
40  * It happens if the LogManager.<clinit> and the Logger.<clinit>
41  * are invoked concurrently on two different threads.
42  * There is a cyclic dependence between the two static initializers:
43  *   1. LogManager.<clinit> instantiate the class RootLogger which
44  *      is a subclass of the Logger class.
45  *      It requires the Logger class initialization to complete.
46  *   2. Logger.<clinit> initializes the field "global", so it
47  *      it makes a call: Logger.getLogger("global").
48  *      Subsequently the LogManager static method getLogManager()
49  *      is called which requires the LogManager class
50  *      initialization to complete.
51  * This cyclic dependence causes a deadlock, so two class
52  * initializations are waiting for each other.
53  * This is a regression test for this bug.
54  */
55 
56 
57 import java.util.Random;
58 import java.util.logging.LogManager;
59 import java.util.logging.Logger;
60 import jdk.test.lib.RandomFactory;
61 
62 public class LoggingDeadlock {
63 
64     private static int preventLoopElision;
65     private static final Random random = RandomFactory.getRandom();
66 
randomDelay()67     public static void randomDelay() {
68         int runs = random.nextInt(1000000);
69         int c = 0;
70 
71         for (int i = 0; i < runs; ++i) {
72             c = c + i;
73         }
74         preventLoopElision = c;
75     }
76 
main(String[] args)77     public static void main(String[] args) throws InterruptedException{
78         Thread t1 = new Thread(new Runnable() {
79             public void run() {
80                 randomDelay();
81                 // Trigger Logger.<clinit>
82                 Logger.getAnonymousLogger();
83             }
84         });
85 
86         Thread t2 = new Thread(new Runnable() {
87             public void run() {
88                 randomDelay();
89                 // Trigger LogManager.<clinit>
90                 LogManager.getLogManager();
91             }
92         });
93 
94         t1.start();
95         t2.start();
96 
97         t1.join();
98         t2.join();
99         System.out.println("\nTest passed");
100     }
101 }
102