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