@@ -156,6 +156,7 @@ public void testNestedAsyncConcat() throws Throwable {
156
156
final CountDownLatch allowThird = new CountDownLatch (1 );
157
157
158
158
final AtomicReference <Thread > parent = new AtomicReference <Thread >();
159
+ final CountDownLatch parentHasStarted = new CountDownLatch (1 );
159
160
Observable <Observable <String >> observableOfObservables = Observable .create (new Observable .OnSubscribeFunc <Observable <String >>() {
160
161
161
162
@ Override
@@ -197,29 +198,22 @@ public void run() {
197
198
}
198
199
}));
199
200
parent .get ().start ();
201
+ parentHasStarted .countDown ();
200
202
return s ;
201
203
}
202
204
});
203
205
204
206
Observable .create (concat (observableOfObservables )).subscribe (observer );
205
207
206
208
// wait for parent to start
207
- while (parent .get () == null ) {
208
- Thread .sleep (1 );
209
- }
209
+ parentHasStarted .await ();
210
210
211
211
try {
212
212
// wait for first 2 async observables to complete
213
- while (o1 .t == null ) {
214
- Thread .sleep (1 );
215
- }
216
- System .out .println ("Thread1 started ... waiting for it to complete ..." );
217
- o1 .t .join ();
218
- while (o2 .t == null ) {
219
- Thread .sleep (1 );
220
- }
221
- System .out .println ("Thread2 started ... waiting for it to complete ..." );
222
- o2 .t .join ();
213
+ System .out .println ("Thread1 is starting ... waiting for it to complete ..." );
214
+ o1 .waitForThreadDone ();
215
+ System .out .println ("Thread2 is starting ... waiting for it to complete ..." );
216
+ o2 .waitForThreadDone ();
223
217
} catch (Throwable e ) {
224
218
throw new RuntimeException ("failed waiting on threads" , e );
225
219
}
@@ -243,11 +237,8 @@ public void run() {
243
237
allowThird .countDown ();
244
238
245
239
try {
246
- while (o3 .t == null ) {
247
- Thread .sleep (1 );
248
- }
249
240
// wait for 3rd to complete
250
- o3 .t . join ();
241
+ o3 .waitForThreadDone ();
251
242
} catch (Throwable e ) {
252
243
throw new RuntimeException ("failed waiting on threads" , e );
253
244
}
@@ -320,9 +311,8 @@ public void testConcatConcurrentWithInfinity() {
320
311
321
312
//Wait for the thread to start up.
322
313
try {
323
- Thread .sleep (25 );
324
- w1 .t .join ();
325
- w2 .t .join ();
314
+ w1 .waitForThreadDone ();
315
+ w2 .waitForThreadDone ();
326
316
} catch (InterruptedException e ) {
327
317
// TODO Auto-generated catch block
328
318
e .printStackTrace ();
@@ -500,6 +490,7 @@ public void unsubscribe() {
500
490
private boolean subscribed = true ;
501
491
private final CountDownLatch once ;
502
492
private final CountDownLatch okToContinue ;
493
+ private final CountDownLatch threadHasStarted = new CountDownLatch (1 );
503
494
private final T seed ;
504
495
private final int size ;
505
496
@@ -553,8 +544,14 @@ public void run() {
553
544
554
545
});
555
546
t .start ();
547
+ threadHasStarted .countDown ();
556
548
return s ;
557
549
}
550
+
551
+ void waitForThreadDone () throws InterruptedException {
552
+ threadHasStarted .await ();
553
+ t .join ();
554
+ }
558
555
}
559
556
@ Test
560
557
public void testMultipleObservers () {
0 commit comments