1 /*
   2  * Copyright (c) 2010, 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 import java.util.Properties;
  25 import java.util.concurrent.CyclicBarrier;
  26 import java.util.concurrent.BrokenBarrierException;
  27 import java.io.IOException;
  28 import java.net.URL;
  29 
  30 /* @test
  31  * @bug 6977738 8029891
  32  * @summary Test ClassLoader.getResource() that should not deadlock
  33  #          if another thread is holding the system properties object
  34  *
  35  * @build GetResource
  36  * @run main GetResource
  37  */
  38 
  39 public class GetResource {
  40     CyclicBarrier go = new CyclicBarrier(2);
  41     CyclicBarrier done = new CyclicBarrier(2);
  42     Thread t1, t2;
  43     public GetResource() {
  44         t1 = new Thread() {
  45             public void run() {
  46                 Properties prop = System.getProperties();
  47                 synchronized (prop) {
  48                     System.out.println("Thread 1 ready");
  49                     try {
  50                         go.await();
  51                         prop.put("property", "value");
  52                         prop.store(System.out, "");
  53                         done.await();   // keep holding the lock until t2 finishes
  54                     } catch (InterruptedException e) {
  55                         throw new RuntimeException(e);
  56                     } catch (BrokenBarrierException e) {
  57                         throw new RuntimeException(e);
  58                     } catch (IOException e) {
  59                         throw new RuntimeException(e);
  60                     }
  61                 }
  62                 System.out.println("Thread 1 exits");
  63             }
  64         };
  65 
  66         t2 = new Thread()  {
  67             public void run() {
  68                 System.out.println("Thread 2 ready");
  69                 try {
  70                     go.await();  // wait until t1 holds the lock of the system properties
  71 
  72                     URL u1 = Thread.currentThread().getContextClassLoader().getResource("unknownresource");
  73                     done.await();
  74                 } catch (InterruptedException e) {
  75                     throw new RuntimeException(e);
  76                 } catch (BrokenBarrierException e) {
  77                     throw new RuntimeException(e);
  78                 }
  79                 System.out.println("Thread 2 exits");
  80             }
  81         };
  82     }
  83 
  84     public void run() throws Exception {
  85         t1.start();
  86         t2.start();
  87         try {
  88             t1.join();
  89         } catch (InterruptedException e) {
  90             e.printStackTrace();
  91             throw e;
  92         }
  93         try {
  94             t2.join();
  95         } catch (InterruptedException e) {
  96             e.printStackTrace();
  97             throw e;
  98         }
  99     }
 100 
 101     public static void main(String[] args) throws Exception {
 102         new GetResource().run();
 103     }
 104 }