Path: blob/master/test/jdk/java/lang/ClassLoader/deadlock/GetResource.java
41153 views
/*1* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Properties;24import java.util.concurrent.CyclicBarrier;25import java.util.concurrent.BrokenBarrierException;26import java.io.IOException;27import java.net.URL;2829/* @test30* @bug 6977738 802989131* @summary Test ClassLoader.getResource() that should not deadlock32# if another thread is holding the system properties object33*34* @build GetResource35* @run main GetResource36*/3738public class GetResource {39CyclicBarrier go = new CyclicBarrier(2);40CyclicBarrier done = new CyclicBarrier(2);41Thread t1, t2;42public GetResource() {43t1 = new Thread() {44public void run() {45Properties prop = System.getProperties();46synchronized (prop) {47System.out.println("Thread 1 ready");48try {49go.await();50prop.put("property", "value");51prop.store(System.out, "");52done.await(); // keep holding the lock until t2 finishes53} catch (InterruptedException e) {54throw new RuntimeException(e);55} catch (BrokenBarrierException e) {56throw new RuntimeException(e);57} catch (IOException e) {58throw new RuntimeException(e);59}60}61System.out.println("Thread 1 exits");62}63};6465t2 = new Thread() {66public void run() {67System.out.println("Thread 2 ready");68try {69go.await(); // wait until t1 holds the lock of the system properties7071URL u1 = Thread.currentThread().getContextClassLoader().getResource("unknownresource");72done.await();73} catch (InterruptedException e) {74throw new RuntimeException(e);75} catch (BrokenBarrierException e) {76throw new RuntimeException(e);77}78System.out.println("Thread 2 exits");79}80};81}8283public void run() throws Exception {84t1.start();85t2.start();86try {87t1.join();88} catch (InterruptedException e) {89e.printStackTrace();90throw e;91}92try {93t2.join();94} catch (InterruptedException e) {95e.printStackTrace();96throw e;97}98}99100public static void main(String[] args) throws Exception {101new GetResource().run();102}103}104105106