Skip to content

Instantly share code, notes, and snippets.

@dagezi
Created January 16, 2017 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagezi/017c16a19364599707b593419ef57343 to your computer and use it in GitHub Desktop.
Save dagezi/017c16a19364599707b593419ef57343 to your computer and use it in GitHub Desktop.
Why can "sub2" retrieve the first item of "src1"?
package hoge;
import rx.*;
import rx.schedulers.*;
import java.util.concurrent.TimeUnit;
// See the items in backpressure bufffer will be genereated
// for the observer who subscribed later?
public class SubscribeLater {
public static void doIt() {
Scheduler sched = Schedulers.newThread();
Observable<Long> src1 =
Observable.interval(1, TimeUnit.MILLISECONDS).
onBackpressureBuffer();
src1.
observeOn(sched).
subscribe(
s -> {
if (s % 10000 == 0) {
System.out.println("Sub 1: " +s);
}
if (false) { // Just drop them!
try {
Thread.sleep(1000); // Blocks
} catch (Exception ex) {
}
}
},
throwable -> {System.err.println(throwable);},
() -> {System.out.println("complete");}
);
try {
Thread.sleep(10000);
} catch (Exception ex) {
}
// Which item will this show?
src1.
subscribe(
s -> {
System.out.println("Sub 2: " +s);
});
try {
Thread.sleep(10000);
} catch (Exception ex) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment