11/*
22 * Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.
3+ * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
34 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45 *
56 * This code is free software; you can redistribute it and/or modify it
4041
4142public class TestNestHostErrorWithMultiThread {
4243
43- public static void main (String args []) {
44+ public static void main (String args []) throws Throwable {
4445 CountDownLatch runLatch = new CountDownLatch (1 );
4546 CountDownLatch startLatch = new CountDownLatch (2 );
4647
47- Runnable test = new Test (runLatch , startLatch );
48-
49- Thread t1 = new Thread (test );
50- Thread t2 = new Thread (test );
48+ TestThread t1 = new TestThread (runLatch , startLatch );
49+ TestThread t2 = new TestThread (runLatch , startLatch );
5150
5251 t1 .start ();
5352 t2 .start ();
@@ -59,39 +58,65 @@ public static void main(String args[]) {
5958
6059 t1 .join ();
6160 t2 .join ();
61+
62+ Throwable threadException = t1 .exception () != null ? t1 .exception ()
63+ : t2 .exception ();
64+ if (threadException != null ) {
65+ Throwable t = threadException ;
66+ try {
67+ throw new Error ("TestThread encountered unexpected exception" , t );
68+ }
69+ catch (OutOfMemoryError oome ) {
70+ // If we encounter an OOME trying to create the wrapper Error,
71+ // then just re-throw the original exception so we report it and
72+ // not the secondary OOME.
73+ throw t ;
74+ }
75+ }
6276 } catch (InterruptedException e ) {
6377 throw new Error ("Unexpected interrupt" );
6478 }
6579 }
6680
67- static class Test implements Runnable {
81+ static class TestThread extends Thread {
6882 private CountDownLatch runLatch ;
6983 private CountDownLatch startLatch ;
84+ private Throwable exception ;
85+
86+ Throwable exception () {
87+ return exception ;
88+ }
7089
71- Test (CountDownLatch runLatch , CountDownLatch startLatch ) {
90+ TestThread (CountDownLatch runLatch , CountDownLatch startLatch ) {
7291 this .runLatch = runLatch ;
7392 this .startLatch = startLatch ;
7493 }
7594
7695 @ Override
7796 public void run () {
97+ // Don't allow any exceptions to escape - the main thread will
98+ // report them.
7899 try {
79- startLatch .countDown ();
80- // Try to have all threads trigger the nesthost check at the same time
81- runLatch .await ();
82- HostNoNestMember h = new HostNoNestMember ();
83- h .test ();
84- throw new Error ("IllegalAccessError was not thrown as expected" );
85- } catch (IllegalAccessError expected ) {
86- String msg = "current type is not listed as a nest member" ;
87- if (!expected .getMessage ().contains (msg )) {
88- throw new Error ("Wrong " + expected .getClass ().getSimpleName () +": \" " +
89- expected .getMessage () + "\" does not contain \" " +
90- msg + "\" " , expected );
100+ try {
101+ startLatch .countDown ();
102+ // Try to have all threads trigger the nesthost check at the same time
103+ runLatch .await ();
104+ HostNoNestMember h = new HostNoNestMember ();
105+ h .test ();
106+ throw new Error ("IllegalAccessError was not thrown as expected" );
107+ } catch (IllegalAccessError expected ) {
108+ String msg = "current type is not listed as a nest member" ;
109+ if (!expected .getMessage ().contains (msg )) {
110+ throw new Error ("Wrong " + expected .getClass ().getSimpleName () +": \" " +
111+ expected .getMessage () + "\" does not contain \" " +
112+ msg + "\" " , expected );
113+ }
114+ System .out .println ("OK - got expected exception: " + expected );
115+ } catch (InterruptedException e ) {
116+ throw new Error ("Unexpected interrupt" , e );
91117 }
92- System .out .println ("OK - got expected exception: " + expected );
93- } catch (InterruptedException e ) {
94- throw new Error ("Unexpected interrupt" );
118+ } catch (Throwable t ) {
119+ exception = t ;
95120 }
96121 }
97122 }
0 commit comments