Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created March 2, 2017 20:46
Show Gist options
  • Select an option

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

Select an option

Save thomasnield/53b4f77a2335c7714977c57bb784451c to your computer and use it in GitHub Desktop.
rxjava2_backpressure_demo
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