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