Path: blob/master/test/jdk/java/io/Serializable/concurrentClassDescLookup/ConcurrentClassDescLookup.java
41153 views
/*1* Copyright (c) 2001, 2019, 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 {32private static final long serialVersionUID = 6319710844400051132L;3334static {35try { Thread.sleep(1000); } catch (InterruptedException ex) {}36}37}3839class Bad implements Serializable {40// explicit suid triggers class initialization during classdesc lookup41private static final long serialVersionUID = 0xBAD;42static {43try { Thread.sleep(1000); } catch (InterruptedException ex) {}44if ("foo".equals("foo")) {45throw new RuntimeException();46}47}48}4950class SuccessfulLookup extends Thread {51Class<?> cl;52long suid;53Object barrier;54boolean ok;5556SuccessfulLookup(Class<?> cl, long suid, Object barrier) {57this.cl = cl;58this.suid = suid;59this.barrier = barrier;60}6162public void run() {63synchronized (barrier) {64try { barrier.wait(); } catch (InterruptedException ex) {}65}66for (int i = 0; i < 100; i++) {67if (ObjectStreamClass.lookup(cl).getSerialVersionUID() != suid) {68return;69}70}71ok = true;72}73}7475class FailingLookup extends Thread {76Class<?> cl;77final Object barrier;78boolean ok;7980FailingLookup(Class<?> cl, Object barrier) {81this.cl = cl;82this.barrier = barrier;83}8485public void run() {86synchronized (barrier) {87try { barrier.wait(); } catch (InterruptedException ex) {}88}89for (int i = 0; i < 100; i++) {90try {91ObjectStreamClass.lookup(cl);92return;93} catch (Throwable th) {94}95}96ok = true;97}98}99100public class ConcurrentClassDescLookup {101public static void main(String[] args) throws Exception {102ClassLoader loader = ConcurrentClassDescLookup.class.getClassLoader();103Class<?> cl = Class.forName("Good", false, loader);104Object barrier = new Object();105SuccessfulLookup[] slookups = new SuccessfulLookup[50];106for (int i = 0; i < slookups.length; i++) {107slookups[i] =108new SuccessfulLookup(cl, 6319710844400051132L, barrier);109slookups[i].start();110}111Thread.sleep(1000);112synchronized (barrier) {113barrier.notifyAll();114}115for (int i = 0; i < slookups.length; i++) {116slookups[i].join();117if (!slookups[i].ok) {118throw new Error();119}120}121122cl = Class.forName("Bad", false, loader);123FailingLookup[] flookups = new FailingLookup[50];124for (int i = 0; i < flookups.length; i++) {125flookups[i] = new FailingLookup(cl, barrier);126flookups[i].start();127}128Thread.sleep(1000);129synchronized (barrier) {130barrier.notifyAll();131}132for (int i = 0; i < slookups.length; i++) {133flookups[i].join();134if (!flookups[i].ok) {135throw new Error();136}137}138}139}140141142