Created
March 2, 2017 20:46
-
-
Save thomasnield/53b4f77a2335c7714977c57bb784451c to your computer and use it in GitHub Desktop.
rxjava2_backpressure_demo
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.Flowable; | |
| import io.reactivex.Observable; | |
| import io.reactivex.schedulers.Schedulers; | |
| import java.util.concurrent.CountDownLatch; | |
| public class Launcher { | |
| public static void main(String[] args) { | |
| final CountDownLatch latch = new CountDownLatch(1); | |
| Flowable.range(1, 999_999_999) | |
| .map(MyItem::new) | |
| .observeOn(Schedulers.io()) | |
| .subscribe(myItem -> { | |
| sleep(50); | |
| System.out.println("Received MyItem " + myItem.id); | |
| }, | |
| Throwable::printStackTrace, | |
| latch::countDown); | |
| try { | |
| latch.await(); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| static void sleep(int milliseconds) { | |
| try { | |
| Thread.sleep(milliseconds); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static final class MyItem { | |
| private final int id; | |
| MyItem(int id) { | |
| this.id = id; | |
| System.out.println("Constructing MyItem " + id); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment