Skip to content
Snippets Groups Projects
Commit 2b128f12 authored by Andrew Lu's avatar Andrew Lu
Browse files

8222884: ConcurrentClassDescLookup.java times out intermittently

Backport-of: bd046d9b9e79e4eea89c72af358961ef6e98e660
parent dae2d6c7
No related branches found
No related tags found
No related merge requests found
/* /*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
*/ */
import java.io.*; import java.io.*;
import java.util.concurrent.CountDownLatch;
class Good implements Serializable { class Good implements Serializable {
private static final long serialVersionUID = 6319710844400051132L; private static final long serialVersionUID = 6319710844400051132L;
...@@ -51,19 +52,20 @@ class Bad implements Serializable { ...@@ -51,19 +52,20 @@ class Bad implements Serializable {
class SuccessfulLookup extends Thread { class SuccessfulLookup extends Thread {
Class<?> cl; Class<?> cl;
long suid; long suid;
Object barrier; final CountDownLatch lookupLatch;
boolean ok; boolean ok;
SuccessfulLookup(Class<?> cl, long suid, Object barrier) { SuccessfulLookup(Class<?> cl, long suid, CountDownLatch lookupLatch) {
this.cl = cl; this.cl = cl;
this.suid = suid; this.suid = suid;
this.barrier = barrier; this.lookupLatch = lookupLatch;
} }
public void run() { public void run() {
synchronized (barrier) { lookupLatch.countDown(); // let others know we are ready
try { barrier.wait(); } catch (InterruptedException ex) {} try {
} lookupLatch.await(); // await for others
} catch (InterruptedException ex) {}
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
if (ObjectStreamClass.lookup(cl).getSerialVersionUID() != suid) { if (ObjectStreamClass.lookup(cl).getSerialVersionUID() != suid) {
return; return;
...@@ -75,18 +77,19 @@ class SuccessfulLookup extends Thread { ...@@ -75,18 +77,19 @@ class SuccessfulLookup extends Thread {
class FailingLookup extends Thread { class FailingLookup extends Thread {
Class<?> cl; Class<?> cl;
final Object barrier; final CountDownLatch lookupLatch;
boolean ok; boolean ok;
FailingLookup(Class<?> cl, Object barrier) { FailingLookup(Class<?> cl, CountDownLatch lookupLatch) {
this.cl = cl; this.cl = cl;
this.barrier = barrier; this.lookupLatch = lookupLatch;
} }
public void run() { public void run() {
synchronized (barrier) { lookupLatch.countDown(); // let others know we are ready
try { barrier.wait(); } catch (InterruptedException ex) {} try {
} lookupLatch.await(); // await for others
} catch (InterruptedException ex) {}
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
try { try {
ObjectStreamClass.lookup(cl); ObjectStreamClass.lookup(cl);
...@@ -102,39 +105,36 @@ public class ConcurrentClassDescLookup { ...@@ -102,39 +105,36 @@ public class ConcurrentClassDescLookup {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ClassLoader loader = ConcurrentClassDescLookup.class.getClassLoader(); ClassLoader loader = ConcurrentClassDescLookup.class.getClassLoader();
Class<?> cl = Class.forName("Good", false, loader); Class<?> cl = Class.forName("Good", false, loader);
Object barrier = new Object(); int numSuccessfulLookups = 50;
SuccessfulLookup[] slookups = new SuccessfulLookup[50]; CountDownLatch sLookupLatch = new CountDownLatch(numSuccessfulLookups);
SuccessfulLookup[] slookups = new SuccessfulLookup[numSuccessfulLookups];
for (int i = 0; i < slookups.length; i++) { for (int i = 0; i < slookups.length; i++) {
slookups[i] = slookups[i] = new SuccessfulLookup(cl, 6319710844400051132L, sLookupLatch);
new SuccessfulLookup(cl, 6319710844400051132L, barrier);
slookups[i].start(); slookups[i].start();
} }
Thread.sleep(1000); System.out.println("awaiting completion of " + slookups.length + " SuccessfulLookup");
synchronized (barrier) {
barrier.notifyAll();
}
for (int i = 0; i < slookups.length; i++) { for (int i = 0; i < slookups.length; i++) {
slookups[i].join(); slookups[i].join();
if (!slookups[i].ok) { if (!slookups[i].ok) {
throw new Error(); throw new Error();
} }
} }
System.out.println("all " + slookups.length + " SuccessfulLookup completed");
cl = Class.forName("Bad", false, loader); cl = Class.forName("Bad", false, loader);
FailingLookup[] flookups = new FailingLookup[50]; int numFailingLookups = 50;
CountDownLatch fLookupLatch = new CountDownLatch(numFailingLookups);
FailingLookup[] flookups = new FailingLookup[numFailingLookups];
for (int i = 0; i < flookups.length; i++) { for (int i = 0; i < flookups.length; i++) {
flookups[i] = new FailingLookup(cl, barrier); flookups[i] = new FailingLookup(cl, fLookupLatch);
flookups[i].start(); flookups[i].start();
} }
Thread.sleep(1000); System.out.println("awaiting completion of " + flookups.length + " FailingLookup");
synchronized (barrier) { for (int i = 0; i < flookups.length; i++) {
barrier.notifyAll();
}
for (int i = 0; i < slookups.length; i++) {
flookups[i].join(); flookups[i].join();
if (!flookups[i].ok) { if (!flookups[i].ok) {
throw new Error(); throw new Error();
} }
} }
System.out.println("all " + flookups.length + " FailingLookup completed");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment