Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created January 14, 2017 02:22
Show Gist options
  • Select an option

  • Save thomasnield/ac2398d2e0ea13a6b3416b10adafa3d6 to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/ac2398d2e0ea13a6b3416b10adafa3d6 to your computer and use it in GitHub Desktop.
Using CountDownLatch with RxJava
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.CountDownLatch;
public class Launcher {
public static void main(String[] args) {
Observable<Integer> source = Observable.range(1,10);
CountDownLatch latch = new CountDownLatch(1);
source.map(i -> i * 100)
.doOnNext(i -> System.out.println("Emitting " + i
+ " on thread " + Thread.currentThread().getName()))
.observeOn(Schedulers.computation())
.map(i -> i * 10)
.subscribe(i -> System.out.println("Received " + i + " on thread "
+ Thread.currentThread().getName()),
Throwable::printStackTrace,
latch::countDown
);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment