Created
January 14, 2017 02:22
-
-
Save thomasnield/ac2398d2e0ea13a6b3416b10adafa3d6 to your computer and use it in GitHub Desktop.
Using CountDownLatch with RxJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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