Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ClassLoader/deadlock/GetResource.java
38828 views
/*1* Copyright (c) 2010, 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 697773831* @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");72URL u2 = Thread.currentThread().getContextClassLoader().getResource("sun/util/resources/CalendarData.class");73if (u2 == null) {74throw new RuntimeException("Test failed: resource not found");75}76done.await();77} catch (InterruptedException e) {78throw new RuntimeException(e);79} catch (BrokenBarrierException e) {80throw new RuntimeException(e);81}82System.out.println("Thread 2 exits");83}84};85}8687public void run() throws Exception {88t1.start();89t2.start();90try {91t1.join();92} catch (InterruptedException e) {93e.printStackTrace();94throw e;95}96try {97t2.join();98} catch (InterruptedException e) {99e.printStackTrace();100throw e;101}102}103104public static void main(String[] args) throws Exception {105new GetResource().run();106}107}108109110