Path: blob/master/test/hotspot/jtreg/compiler/jsr292/ConcurrentClassLoadingTest.java
41149 views
/*1* Copyright (c) 2013, 2020, 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* @key randomness26* @bug 802259527* @summary JSR292: deadlock during class loading of MethodHandles, MethodHandleImpl & MethodHandleNatives28* @library /test/lib /29* @modules java.base/jdk.internal.misc30* java.management31*32* @run main/othervm compiler.jsr292.ConcurrentClassLoadingTest33*/3435package compiler.jsr292;3637import jdk.test.lib.Utils;3839import java.util.ArrayList;40import java.util.Arrays;41import java.util.List;42import java.util.Random;43import java.util.concurrent.BrokenBarrierException;44import java.util.concurrent.CyclicBarrier;4546public class ConcurrentClassLoadingTest {47int numThreads = 0;48CyclicBarrier l;49private static final Random rand = Utils.getRandomInstance();5051public static void main(String[] args) throws Throwable {52ConcurrentClassLoadingTest test = new ConcurrentClassLoadingTest();53test.parseArgs(args);54test.run();55}5657void parseArgs(String[] args) {58int i = 0;59while (i < args.length) {60String flag = args[i];61switch(flag) {62case "-numThreads":63numThreads = Integer.parseInt(args[++i]);64break;65default:66throw new Error("Unknown flag: " + flag);67}68++i;69}70}7172void init() {73if (numThreads == 0) {74numThreads = Runtime.getRuntime().availableProcessors();75}7677l = new CyclicBarrier(numThreads + 1);7879System.out.printf("Threads: %d\n", numThreads);80}8182final List<Loader> loaders = new ArrayList<>();8384void prepare() {85List<String> c = new ArrayList<>(Arrays.asList(classNames));8687// Split classes between loading threads88int count = (classNames.length / numThreads) + 1;89for (int t = 0; t < numThreads; t++) {90List<String> sel = new ArrayList<>();9192System.out.printf("Thread #%d:\n", t);93for (int i = 0; i < count; i++) {94if (c.isEmpty()) {95break;96}9798int k = rand.nextInt(c.size());99String elem = c.remove(k);100sel.add(elem);101System.out.printf("\t%s\n", elem);102}103loaders.add(new Loader(sel));104}105106// Print diagnostic info when the test hangs107Runtime.getRuntime().addShutdownHook(new Thread() {108public void run() {109boolean alive = false;110for (Loader l : loaders) {111if (!l.isAlive()) continue;112113if (!alive) {114System.out.println("Some threads are still alive:");115alive = true;116}117118System.out.println(l.getName());119for (StackTraceElement elem : l.getStackTrace()) {120System.out.println("\t"+elem.toString());121}122}123}124});125}126127public void run() throws Throwable {128init();129prepare();130131for (Loader loader : loaders) {132loader.start();133}134135l.await();136137for (Loader loader : loaders) {138loader.join();139}140}141142class Loader extends Thread {143List<String> classes;144145public Loader(List<String> classes) {146this.classes = classes;147setDaemon(true);148}149150@Override151public void run() {152try {153l.await();154155for (String name : classes) {156Class.forName(name).getName();157}158} catch (ClassNotFoundException | BrokenBarrierException | InterruptedException e) {159throw new Error(e);160}161}162}163164final static String[] classNames = {165"java.lang.invoke.CallSite",166"java.lang.invoke.ConstantCallSite",167"java.lang.invoke.LambdaConversionException",168"java.lang.invoke.LambdaMetafactory",169"java.lang.invoke.MethodHandle",170"java.lang.invoke.MethodHandleInfo",171"java.lang.invoke.MethodHandleProxies",172"java.lang.invoke.MethodHandles",173"java.lang.invoke.MethodType",174"java.lang.invoke.MutableCallSite",175"java.lang.invoke.SerializedLambda",176"java.lang.invoke.SwitchPoint",177"java.lang.invoke.VolatileCallSite",178"java.lang.invoke.WrongMethodTypeException"179};180}181182183