Created
January 16, 2017 07:00
-
-
Save dagezi/017c16a19364599707b593419ef57343 to your computer and use it in GitHub Desktop.
Why can "sub2" retrieve the first item of "src1"?
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
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