Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Proxy/ProxyRace.java
38828 views
/*1* Copyright (c) 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*/2223import java.lang.reflect.Proxy;24import java.util.concurrent.ExecutorService;25import java.util.concurrent.Executors;26import java.util.concurrent.Phaser;27import java.util.concurrent.TimeUnit;28import java.util.concurrent.atomic.AtomicInteger;2930/**31* @test32* @bug 817472933* @summary Proxy.getProxyClass() / Proxy.isProxyClass() race detector34* @run main ProxyRace35* @author plevart36*/3738public class ProxyRace {3940static final int threads = 8;4142static volatile ClassLoader classLoader;43static volatile boolean terminate;44static final AtomicInteger racesDetected = new AtomicInteger();4546public static void main(String[] args) throws Exception {4748Phaser phaser = new Phaser(threads) {49@Override50protected boolean onAdvance(int phase, int registeredParties) {51// install new ClassLoader on each advance52classLoader = new CL();53return terminate;54}55};5657ExecutorService exe = Executors.newFixedThreadPool(threads);5859for (int i = 0; i < threads; i++) {60exe.execute(() -> {61while (phaser.arriveAndAwaitAdvance() >= 0) {62Class<?> proxyClass = Proxy.getProxyClass(classLoader, Runnable.class);63if (!Proxy.isProxyClass(proxyClass)) {64racesDetected.incrementAndGet();65}66}67});68}6970Thread.sleep(5000L);7172terminate = true;73exe.shutdown();74exe.awaitTermination(5L, TimeUnit.SECONDS);7576System.out.println(racesDetected.get() + " races detected");77if (racesDetected.get() != 0) {78throw new RuntimeException(racesDetected.get() + " races detected");79}80}8182static class CL extends ClassLoader {83public CL() {84super(ClassLoader.getSystemClassLoader());85}86}87}888990