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