Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ClassLoader/deadlock/Starter.java
38828 views
/*1* Copyright (c) 2009, 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.net.MalformedURLException;24import java.net.URL;2526public class Starter implements Runnable {2728private String id;29private DelegatingLoader dl;30private String startClass;3132private static DelegatingLoader saLoader, sbLoader;3334public static void log(String line) {35System.out.println(line);36}3738public static void main(String[] args) {39URL[] urlsa = new URL[1];40URL[] urlsb = new URL[1];41try {42String testDir = System.getProperty("test.classes", ".");43String sep = System.getProperty("file.separator");44urlsa[0] = new URL("file://" + testDir + sep + "SA" + sep);45urlsb[0] = new URL("file://" + testDir + sep + "SB" + sep);46} catch (MalformedURLException e) {47e.printStackTrace();48}49// Set up Classloader delegation hierarchy50saLoader = new DelegatingLoader(urlsa);51sbLoader = new DelegatingLoader(urlsb);5253String[] saClasses = { "comSA.SupBob", "comSA.Alice" };54String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };5556saLoader.setDelegate(sbClasses, sbLoader);57sbLoader.setDelegate(saClasses, saLoader);5859// test one-way delegate60String testType = args[0];61if (testType.equals("one-way")) {62test("comSA.Alice", "comSA.SupBob");63} else if (testType.equals("cross")) {64// test cross delegate65test("comSA.Alice", "comSB.Bob");66} else {67System.out.println("ERROR: unsupported - " + testType);68}69}7071private static void test(String clsForSA, String clsForSB) {72Starter ia = new Starter("SA", saLoader, clsForSA);73Starter ib = new Starter("SB", sbLoader, clsForSB);74new Thread(ia).start();75new Thread(ib).start();76}7778public static void sleep() {79try {80Thread.sleep(500);81} catch (InterruptedException e) {82e.printStackTrace();83log("Thread interrupted");84}85}8687private Starter(String id, DelegatingLoader dl, String startClass) {88this.id = id;89this.dl = dl;90this.startClass = startClass;91}9293public void run() {94log("Spawned thread " + id + " running");95try {96// To mirror the WAS deadlock, need to ensure class load97// is routed via the VM.98Class.forName(startClass, true, dl);99} catch (ClassNotFoundException e) {100e.printStackTrace();101}102log("Thread " + id + " terminating");103}104}105106107