Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/ThreadSafety.java
38828 views
/*1* Copyright 2014 Google Inc. 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 8062771 801623626* @summary Test publication of Class objects via a data race27* @run testng ThreadSafety28*/2930import java.net.URL;31import java.net.URLClassLoader;32import java.util.Collections;33import java.util.concurrent.BrokenBarrierException;34import java.util.concurrent.Callable;35import java.util.concurrent.CyclicBarrier;36import java.util.concurrent.ExecutionException;37import java.util.concurrent.ExecutorService;38import java.util.concurrent.Executors;39import java.util.concurrent.Future;40import java.util.concurrent.TimeoutException;41import static java.util.concurrent.TimeUnit.SECONDS;42import static org.testng.Assert.*;43import org.testng.annotations.Test;4445/**46* A test resulting from an attempt to repro this failure (in guice):47*48* java.lang.NullPointerException49* at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)50* at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)51* at sun.reflect.generics.repository.ClassRepository.getSuperclass(ClassRepository.java:84)52* at java.lang.Class.getGenericSuperclass(Class.java:692)53* at com.google.inject.TypeLiteral.getSuperclassTypeParameter(TypeLiteral.java:99)54* at com.google.inject.TypeLiteral.<init>(TypeLiteral.java:79)55*56* However, as one would expect with thread safety problems in reflection, these57* are very hard to reproduce. This very test has never been observed to fail,58* but a similar test has been observed to fail about once in 2000 executions59* (about once every 6 CPU-hours), in jdk7 only. It appears to be fixed in jdk8+ by:60*61* 8016236: Class.getGenericInterfaces performance improvement.62* (by making Class.genericInfo volatile)63*/64public class ThreadSafety {65public static class EmptyClass {66public static class EmptyGenericSuperclass<T> {}67public static class EmptyGenericSubclass<T> extends EmptyGenericSuperclass<T> {}68}6970/** published via data race */71private Class<?> racyClass = Object.class;7273private URL[] urls = ((URLClassLoader) ThreadSafety.class.getClassLoader()).getURLs();7475private Class<?> createNewEmptyGenericSubclassClass() throws Exception {76URLClassLoader ucl = new URLClassLoader(urls, null);77return Class.forName("ThreadSafety$EmptyClass$EmptyGenericSubclass", true, ucl);78}7980@Test81public void testRacy_getGenericSuperclass() throws Exception {82final int nThreads = 10;83final int iterations = 30;84final int timeout = 10;85final CyclicBarrier newCycle = new CyclicBarrier(nThreads);86final Callable<Void> task = new Callable<Void>() {87public Void call() throws Exception {88for (int i = 0; i < iterations; i++) {89final int threadId;90try {91threadId = newCycle.await(timeout, SECONDS);92} catch (BrokenBarrierException e) {93return null;94}95for (int j = 0; j < iterations; j++) {96// one thread publishes the class object via a data97// race, for the other threads to consume.98if (threadId == 0) {99racyClass = createNewEmptyGenericSubclassClass();100} else {101racyClass.getGenericSuperclass();102}103}104}105return null;106}};107108final ExecutorService pool = Executors.newFixedThreadPool(nThreads);109try {110for (Future<Void> future :111pool.invokeAll(Collections.nCopies(nThreads, task))) {112try {113future.get(iterations * timeout, SECONDS);114} catch (ExecutionException e) {115// ignore "collateral damage"116if (!(e.getCause() instanceof BrokenBarrierException)117&&118!(e.getCause() instanceof TimeoutException)) {119throw e;120}121}122}123} finally {124pool.shutdownNow();125assertTrue(pool.awaitTermination(2 * timeout, SECONDS));126}127}128}129130131