Path: blob/master/test/jdk/java/lang/ClassLoader/deadlock/DelegateTest.java
41153 views
/*1* Copyright (c) 2009, 2017, 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*/2223/*24* @test25* @bug 473512626* @summary (cl) ClassLoader.loadClass locks all instances in chain when delegating27* @modules java.base/java.lang:open28* jdk.compiler29* @library /test/lib30* @build jdk.test.lib.compiler.CompilerUtils31* @run main/othervm -Xlog:class+load DelegateTest one-way32* @run main/othervm -Xlog:class+load DelegateTest cross33*/3435import java.io.File;36import java.net.MalformedURLException;37import java.net.URL;38import java.nio.file.Path;39import java.nio.file.Paths;4041import jdk.test.lib.compiler.CompilerUtils;4243public class DelegateTest implements Runnable {4445private static final Path TEST_DIR = Paths.get(System.getProperty("user.dir", "."));46private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), "src");4748private String id;49private DelegatingLoader dl;50private String startClass;5152private static DelegatingLoader saLoader, sbLoader;5354public static void log(String line) {55System.out.println(line);56}5758public static void main(String[] args) throws Exception {59if (!CompilerUtils.compile(SRC_DIR, TEST_DIR)) {60throw new RuntimeException("Failed to compile "61+ SRC_DIR.toAbsolutePath().toString());62}6364URL[] url = new URL[1];65try {66url[0] = new URL("file://" + TEST_DIR + File.separator);67} catch (MalformedURLException e) {68e.printStackTrace();69}70// Set up Classloader delegation hierarchy71saLoader = new DelegatingLoader(url);72sbLoader = new DelegatingLoader(url);7374String[] saClasses = { "comSA.SupBob", "comSA.Alice" };75String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };7677saLoader.setDelegate(sbClasses, sbLoader);78sbLoader.setDelegate(saClasses, saLoader);7980// test one-way delegate81String testType = args[0];82if (testType.equals("one-way")) {83test("comSA.Alice", "comSA.SupBob");84} else if (testType.equals("cross")) {85// test cross delegate86test("comSA.Alice", "comSB.Bob");87} else {88System.out.println("ERROR: unsupported - " + testType);89}90}9192private static void test(String clsForSA, String clsForSB) throws InterruptedException {93DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA);94DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB);95Thread ta = new Thread(ia);96Thread tb = new Thread(ib);97ta.start();98tb.start();99ta.join();100tb.join();101}102103public static void sleep() {104try {105Thread.sleep(500);106} catch (InterruptedException e) {107e.printStackTrace();108log("Thread interrupted");109}110}111112private DelegateTest(String id, DelegatingLoader dl, String startClass) {113this.id = id;114this.dl = dl;115this.startClass = startClass;116}117118public void run() {119log("Spawned thread " + id + " running");120try {121// To mirror the WAS deadlock, need to ensure class load122// is routed via the VM.123Class.forName(startClass, true, dl);124} catch (ClassNotFoundException e) {125e.printStackTrace();126}127log("Thread " + id + " terminating");128}129}130131132