Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/concurrentClassDescLookup/ConcurrentClassDescLookup.java
38828 views
/*1* Copyright (c) 2001, 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/* @test24* @summary Verify that concurrent class descriptor lookups function properly,25* even when class descriptor initialization is slow or throws an26* exception.27*/2829import java.io.*;3031class Good implements Serializable {32static {33try { Thread.sleep(1000); } catch (InterruptedException ex) {}34}35}3637class Bad implements Serializable {38// explicit suid triggers class initialization during classdesc lookup39private static final long serialVersionUID = 0xBAD;40static {41try { Thread.sleep(1000); } catch (InterruptedException ex) {}42if ("foo".equals("foo")) {43throw new RuntimeException();44}45}46}4748class SuccessfulLookup extends Thread {49Class cl;50long suid;51Object barrier;52boolean ok;5354SuccessfulLookup(Class cl, long suid, Object barrier) {55this.cl = cl;56this.suid = suid;57this.barrier = barrier;58}5960public void run() {61synchronized (barrier) {62try { barrier.wait(); } catch (InterruptedException ex) {}63}64for (int i = 0; i < 100; i++) {65if (ObjectStreamClass.lookup(cl).getSerialVersionUID() != suid) {66return;67}68}69ok = true;70}71}7273class FailingLookup extends Thread {74Class cl;75Object barrier;76boolean ok;7778FailingLookup(Class cl, Object barrier) {79this.cl = cl;80this.barrier = barrier;81}8283public void run() {84synchronized (barrier) {85try { barrier.wait(); } catch (InterruptedException ex) {}86}87for (int i = 0; i < 100; i++) {88try {89ObjectStreamClass.lookup(cl);90return;91} catch (Throwable th) {92}93}94ok = true;95}96}9798public class ConcurrentClassDescLookup {99public static void main(String[] args) throws Exception {100ClassLoader loader = ConcurrentClassDescLookup.class.getClassLoader();101Class cl = Class.forName("Good", false, loader);102Object barrier = new Object();103SuccessfulLookup[] slookups = new SuccessfulLookup[50];104for (int i = 0; i < slookups.length; i++) {105slookups[i] =106new SuccessfulLookup(cl, 6319710844400051132L, barrier);107slookups[i].start();108}109Thread.sleep(1000);110synchronized (barrier) {111barrier.notifyAll();112}113for (int i = 0; i < slookups.length; i++) {114slookups[i].join();115if (!slookups[i].ok) {116throw new Error();117}118}119120cl = Class.forName("Bad", false, loader);121FailingLookup[] flookups = new FailingLookup[50];122for (int i = 0; i < flookups.length; i++) {123flookups[i] = new FailingLookup(cl, barrier);124flookups[i].start();125}126Thread.sleep(1000);127synchronized (barrier) {128barrier.notifyAll();129}130for (int i = 0; i < slookups.length; i++) {131flookups[i].join();132if (!flookups[i].ok) {133throw new Error();134}135}136}137}138139140