11/*
2- * Copyright (c) 2016, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2016, 2025 , 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
3636import java .util .concurrent .CountDownLatch ;
3737
3838public class StressNativeSignal {
39- private UDPThread udpThread ;
40- private ServerSocketThread serverSocketThread ;
39+ private final UDPThread udpThread ;
40+ private final ServerSocketThread serverSocketThread ;
4141
42- StressNativeSignal () {
42+ StressNativeSignal () throws IOException {
4343 serverSocketThread = initServerSocketThread ();
44- if (serverSocketThread != null ) {
45- serverSocketThread .start ();
46- }
44+ serverSocketThread .start ();
4745
4846 udpThread = initUDPThread ();
49- if (udpThread != null ) {
50- udpThread .start ();
51- }
47+ udpThread .start ();
5248 }
5349
54- private UDPThread initUDPThread () {
55- UDPThread aUDPThread = null ;
56- try {
57- aUDPThread = new UDPThread ();
58- } catch (Exception z ) {
59- System .err .println ("failed to create and start a UDPThread" );
60- z .printStackTrace ();
61- }
62- return aUDPThread ;
50+ private UDPThread initUDPThread () throws IOException {
51+ return new UDPThread ();
6352 }
6453
65- private ServerSocketThread initServerSocketThread () {
66- ServerSocketThread aServerSocketThread = null ;
67- try {
68- aServerSocketThread = new ServerSocketThread ();
69-
70- } catch (Exception z ) {
71- System .err .println ("failed to create and start a ServerSocketThread" );
72- z .printStackTrace ();
73- }
74- return aServerSocketThread ;
54+ private ServerSocketThread initServerSocketThread () throws IOException {
55+ return new ServerSocketThread ();
7556 }
7657
7758 public static void main (String [] args ) throws Throwable {
@@ -80,46 +61,39 @@ public static void main(String[] args) throws Throwable {
8061 test .shutdown ();
8162 }
8263
83- public void shutdown () {
84- if (( udpThread != null ) && udpThread .isAlive ()) {
64+ public void shutdown () throws InterruptedException , IOException {
65+ if (udpThread != null && udpThread .isAlive ()) {
8566 udpThread .terminate ();
86- try {
87- udpThread .join ();
88- } catch (Exception z ) {
89- z .printStackTrace (System .err );
90- }
67+ udpThread .join ();
68+
9169 } else {
9270 System .out .println ("UDPThread test scenario was not run" );
9371 }
9472
95- if (( serverSocketThread != null ) && ( serverSocketThread .isAlive () )) {
73+ if (serverSocketThread != null && serverSocketThread .isAlive ()) {
9674 serverSocketThread .terminate ();
97- try {
98- serverSocketThread .join ();
99- } catch (Exception z ) {
100- z .printStackTrace (System .err );
101- }
75+ serverSocketThread .join ();
10276 } else {
10377 System .out .println ("ServerSocketThread test scenario was not run" );
10478 }
10579 }
10680
107- public void waitForTestThreadsToStart () {
108- if (( udpThread != null ) && udpThread .isAlive ()) {
81+ public void waitForTestThreadsToStart () throws InterruptedException {
82+ if (udpThread != null && udpThread .isAlive ()) {
10983 udpThread .waitTestThreadStart ();
11084 }
111- if (( serverSocketThread != null ) && ( serverSocketThread .isAlive () )) {
85+ if (serverSocketThread != null && serverSocketThread .isAlive ()) {
11286 serverSocketThread .waitTestThreadStart ();
11387 }
11488 }
11589
11690 public class ServerSocketThread extends Thread {
11791 private volatile boolean shouldTerminate ;
118- private ServerSocket socket ;
92+ private final ServerSocket socket ;
11993 private final CountDownLatch threadStarted = new CountDownLatch (1 );
12094
121- public ServerSocketThread () throws Exception {
122- socket = new ServerSocket (1122 );
95+ public ServerSocketThread () throws IOException {
96+ socket = new ServerSocket (0 );
12397 }
12498
12599 public void run () {
@@ -129,7 +103,7 @@ public void run() {
129103 Socket client = socket .accept ();
130104 client .close ();
131105 throw new RuntimeException ("Unexpected return from accept call" );
132- } catch (Exception z ) {
106+ } catch (IOException z ) {
133107 System .err .println ("ServerSocketThread: caught exception " + z .getClass ().getName ());
134108 if (!shouldTerminate ) {
135109 z .printStackTrace (System .err );
@@ -141,7 +115,7 @@ public void terminate() {
141115 shouldTerminate = true ;
142116 try {
143117 socket .close ();
144- } catch (Exception z ) {
118+ } catch (IOException z ) {
145119 z .printStackTrace (System .err );
146120 // ignore
147121 }
@@ -150,23 +124,22 @@ public void terminate() {
150124 public void waitTestThreadStart () {
151125 try {
152126 threadStarted .await ();
153- } catch (Exception z ) {
127+ } catch (InterruptedException z ) {
154128 z .printStackTrace (System .err );
155129 // ignore
156130 }
157131 }
158132 }
159133
160134 public class UDPThread extends Thread {
161- private DatagramChannel channel ;
135+ private final DatagramChannel channel ;
162136 private volatile boolean shouldTerminate ;
163137 private final CountDownLatch threadStarted = new CountDownLatch (1 );
164138
165- public UDPThread () throws Exception {
166-
139+ public UDPThread () throws IOException {
167140 channel = DatagramChannel .open ();
168141 channel .setOption (StandardSocketOptions .SO_RCVBUF , 6553600 );
169- channel .bind (new InetSocketAddress (19870 ));
142+ channel .bind (new InetSocketAddress (0 ));
170143 }
171144
172145 @ Override
@@ -191,7 +164,7 @@ public void terminate() {
191164 shouldTerminate = true ;
192165 try {
193166 channel .close ();
194- } catch (Exception z ) {
167+ } catch (IOException z ) {
195168 System .err .println ("UDPThread: caught exception " + z .getClass ().getName ());
196169 z .printStackTrace (System .err );
197170 // ignore
@@ -201,7 +174,7 @@ public void terminate() {
201174 public void waitTestThreadStart () {
202175 try {
203176 threadStarted .await ();
204- } catch (Exception z ) {
177+ } catch (InterruptedException z ) {
205178 z .printStackTrace (System .err );
206179 // ignore
207180 }
0 commit comments