4
4
import java .lang .management .ThreadMXBean ;
5
5
import java .util .Arrays ;
6
6
import java .util .concurrent .Callable ;
7
+ import java .util .concurrent .CountDownLatch ;
7
8
import java .util .concurrent .ExecutionException ;
8
9
import java .util .concurrent .FutureTask ;
9
10
import java .util .concurrent .TimeUnit ;
@@ -37,11 +38,13 @@ public FailOnTimeout(Statement originalStatement, long timeout, TimeUnit unit, b
37
38
38
39
@ Override
39
40
public void evaluate () throws Throwable {
40
- FutureTask <Throwable > task = new FutureTask <Throwable >(new CallableStatement ());
41
+ CallableStatement callable = new CallableStatement ();
42
+ FutureTask <Throwable > task = new FutureTask <Throwable >(callable );
41
43
threadGroup = new ThreadGroup ("FailOnTimeoutGroup" );
42
44
Thread thread = new Thread (threadGroup , task , "Time-limited test" );
43
45
thread .setDaemon (true );
44
46
thread .start ();
47
+ callable .awaitStarted ();
45
48
Throwable throwable = getResult (task , thread );
46
49
if (throwable != null ) {
47
50
throw throwable ;
@@ -204,8 +207,11 @@ private long cpuTime (Thread thr) {
204
207
}
205
208
206
209
private class CallableStatement implements Callable <Throwable > {
210
+ private final CountDownLatch startLatch = new CountDownLatch (1 );
211
+
207
212
public Throwable call () throws Exception {
208
213
try {
214
+ startLatch .countDown ();
209
215
originalStatement .evaluate ();
210
216
} catch (Exception e ) {
211
217
throw e ;
@@ -214,5 +220,9 @@ public Throwable call() throws Exception {
214
220
}
215
221
return null ;
216
222
}
223
+
224
+ public void awaitStarted () throws InterruptedException {
225
+ startLatch .await ();
226
+ }
217
227
}
218
228
}
0 commit comments