1
1
/*
2
- * Copyright (c) 2007, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2007, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
25
25
26
26
import nsk .share .log .Log ;
27
27
import nsk .share .TestFailure ;
28
+
29
+ import java .io .PrintStream ;
28
30
import java .lang .management .LockInfo ;
29
31
import java .lang .management .MonitorInfo ;
30
32
import java .lang .management .ThreadInfo ;
@@ -203,6 +205,14 @@ public static void printStackTrace(Log log, ThreadInfo ti) {
203
205
204
206
}
205
207
208
+ public static void printStackTrace (PrintStream out , Thread t ) {
209
+ StackTraceElement [] stacktrace = t .getStackTrace ();
210
+ for (int i = 0 ; i < stacktrace .length ; i ++) {
211
+ StackTraceElement ste = stacktrace [i ];
212
+ out .println (INDENT + "at " + ste .toString ());
213
+ }
214
+ }
215
+
206
216
/**
207
217
* Dump information about threads.
208
218
*
@@ -218,20 +228,33 @@ public static void threadDump(Log log, ThreadInfo[] tinfos) {
218
228
}
219
229
}
220
230
231
+ // Most uses of waitThreadState usually succeed without retries.
232
+ // These values should avoid spurious failures.
221
233
public final static int waitThreadStateRetries = 10 ;
222
- public final static long waitThreadStateSleepTime = 100 ;
234
+ public final static long waitThreadStateSleepTime = 1000 ;
223
235
224
236
public static void waitThreadState (Thread thread , Thread .State state ) {
225
237
int retries = 0 ;
226
- long ctime = System .currentTimeMillis ();
238
+ long startTime = System .currentTimeMillis ();
239
+ long elapsedTime = 0 ;
227
240
while (thread .getState () != state ) {
228
- if (retries ++ > waitThreadStateRetries )
229
- throw new TestFailure ("Thread " + thread + " with current state " + thread .getState () + " did not reach state " + state + " with number of retries: " + retries + ", time: " + (System .currentTimeMillis () - ctime ));
241
+ if (retries ++ > waitThreadStateRetries ) {
242
+ // Failed to see desired state, give info to help diagnose the isuse.
243
+ // Show the thread, and fail with Exception showing retries and time taken.
244
+ elapsedTime = System .currentTimeMillis () - startTime ;
245
+ System .err .println ("waitThreadState: problem thread: " + thread );
246
+ printStackTrace (System .err , thread );
247
+ throw new TestFailure ("Thread " + thread + " with current state " + thread .getState () + " did not reach state "
248
+ + state + " with number of retries: " + retries + ", time: " + elapsedTime );
249
+ }
230
250
try {
231
251
Thread .sleep (waitThreadStateSleepTime );
232
252
} catch (InterruptedException e ) {
233
253
}
234
254
}
255
+ // Show retries and time, so we know what "normal" looks like:
256
+ elapsedTime = System .currentTimeMillis () - startTime ;
257
+ System .out .println ("waitThreadState: OK. retries: " + retries + " time: " + elapsedTime );
235
258
}
236
259
237
260
/**
0 commit comments