11/*
2- * Copyright (c) 2001, 2019 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2001, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2828 */
2929
3030import java .io .*;
31+ import java .util .concurrent .CountDownLatch ;
3132
3233class Good implements Serializable {
3334 private static final long serialVersionUID = 6319710844400051132L ;
@@ -51,19 +52,20 @@ class Bad implements Serializable {
5152class SuccessfulLookup extends Thread {
5253 Class <?> cl ;
5354 long suid ;
54- Object barrier ;
55+ final CountDownLatch lookupLatch ;
5556 boolean ok ;
5657
57- SuccessfulLookup (Class <?> cl , long suid , Object barrier ) {
58+ SuccessfulLookup (Class <?> cl , long suid , CountDownLatch lookupLatch ) {
5859 this .cl = cl ;
5960 this .suid = suid ;
60- this .barrier = barrier ;
61+ this .lookupLatch = lookupLatch ;
6162 }
6263
6364 public void run () {
64- synchronized (barrier ) {
65- try { barrier .wait (); } catch (InterruptedException ex ) {}
66- }
65+ lookupLatch .countDown (); // let others know we are ready
66+ try {
67+ lookupLatch .await (); // await for others
68+ } catch (InterruptedException ex ) {}
6769 for (int i = 0 ; i < 100 ; i ++) {
6870 if (ObjectStreamClass .lookup (cl ).getSerialVersionUID () != suid ) {
6971 return ;
@@ -75,18 +77,19 @@ public void run() {
7577
7678class FailingLookup extends Thread {
7779 Class <?> cl ;
78- final Object barrier ;
80+ final CountDownLatch lookupLatch ;
7981 boolean ok ;
8082
81- FailingLookup (Class <?> cl , Object barrier ) {
83+ FailingLookup (Class <?> cl , CountDownLatch lookupLatch ) {
8284 this .cl = cl ;
83- this .barrier = barrier ;
85+ this .lookupLatch = lookupLatch ;
8486 }
8587
8688 public void run () {
87- synchronized (barrier ) {
88- try { barrier .wait (); } catch (InterruptedException ex ) {}
89- }
89+ lookupLatch .countDown (); // let others know we are ready
90+ try {
91+ lookupLatch .await (); // await for others
92+ } catch (InterruptedException ex ) {}
9093 for (int i = 0 ; i < 100 ; i ++) {
9194 try {
9295 ObjectStreamClass .lookup (cl );
@@ -102,39 +105,36 @@ public class ConcurrentClassDescLookup {
102105 public static void main (String [] args ) throws Exception {
103106 ClassLoader loader = ConcurrentClassDescLookup .class .getClassLoader ();
104107 Class <?> cl = Class .forName ("Good" , false , loader );
105- Object barrier = new Object ();
106- SuccessfulLookup [] slookups = new SuccessfulLookup [50 ];
108+ int numSuccessfulLookups = 50 ;
109+ CountDownLatch sLookupLatch = new CountDownLatch (numSuccessfulLookups );
110+ SuccessfulLookup [] slookups = new SuccessfulLookup [numSuccessfulLookups ];
107111 for (int i = 0 ; i < slookups .length ; i ++) {
108- slookups [i ] =
109- new SuccessfulLookup (cl , 6319710844400051132L , barrier );
112+ slookups [i ] = new SuccessfulLookup (cl , 6319710844400051132L , sLookupLatch );
110113 slookups [i ].start ();
111114 }
112- Thread .sleep (1000 );
113- synchronized (barrier ) {
114- barrier .notifyAll ();
115- }
115+ System .out .println ("awaiting completion of " + slookups .length + " SuccessfulLookup" );
116116 for (int i = 0 ; i < slookups .length ; i ++) {
117117 slookups [i ].join ();
118118 if (!slookups [i ].ok ) {
119119 throw new Error ();
120120 }
121121 }
122-
122+ System . out . println ( "all " + slookups . length + " SuccessfulLookup completed" );
123123 cl = Class .forName ("Bad" , false , loader );
124- FailingLookup [] flookups = new FailingLookup [50 ];
124+ int numFailingLookups = 50 ;
125+ CountDownLatch fLookupLatch = new CountDownLatch (numFailingLookups );
126+ FailingLookup [] flookups = new FailingLookup [numFailingLookups ];
125127 for (int i = 0 ; i < flookups .length ; i ++) {
126- flookups [i ] = new FailingLookup (cl , barrier );
128+ flookups [i ] = new FailingLookup (cl , fLookupLatch );
127129 flookups [i ].start ();
128130 }
129- Thread .sleep (1000 );
130- synchronized (barrier ) {
131- barrier .notifyAll ();
132- }
133- for (int i = 0 ; i < slookups .length ; i ++) {
131+ System .out .println ("awaiting completion of " + flookups .length + " FailingLookup" );
132+ for (int i = 0 ; i < flookups .length ; i ++) {
134133 flookups [i ].join ();
135134 if (!flookups [i ].ok ) {
136135 throw new Error ();
137136 }
138137 }
138+ System .out .println ("all " + flookups .length + " FailingLookup completed" );
139139 }
140140}
0 commit comments