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
| Observable<Long> cold = Observable.interval(200, TimeUnit.MILLISECONDS); | |
| cold.subscribe(i -> System.out.println("First: " + i)); | |
| Thread.sleep(500); | |
| cold.subscribe(i -> System.out.println("Second: " + i)); |
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
| ConnectableObservable<Long> cold = Observable.interval(200, TimeUnit.MILLISECONDS).publish(); | |
| cold.connect(); | |
| cold.subscribe(i -> System.out.println("First: " + i)); | |
| Thread.sleep(500); | |
| cold.subscribe(i -> System.out.println("Second: " + i)); |
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
| ConnectableObservable<Long> cold = Observable.interval(200, TimeUnit.MILLISECONDS).replay(); | |
| Subscription s = cold.connect(); | |
| System.out.println("Subscribe first"); | |
| Subscription s1 = cold.subscribe(i -> System.out.println("First: " + i)); | |
| Thread.sleep(700); | |
| System.out.println("Subscribe second"); | |
| Subscription s2 = cold.subscribe(i -> System.out.println("Second: " + i)); | |
| Thread.sleep(500); |
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
| Observable<Long> obs = Observable.interval(100, TimeUnit.MILLISECONDS) | |
| .take(5) | |
| .cache(); | |
| Thread.sleep(500); | |
| obs.subscribe(i -> System.out.println("First: " + i)); | |
| Thread.sleep(300); | |
| obs.subscribe(i -> System.out.println("Second: " + i)); |
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
| final BehaviorSubject<Integer> subject = BehaviorSubject.create(); | |
| subject.subscribe(i -> { | |
| System.out.println("Received " + i + " on " + Thread.currentThread().getId()); | |
| }); | |
| int[] i = {1}; // naughty side-effects for examples only ;) | |
| Runnable r = () -> { | |
| synchronized(i) { | |
| System.out.println("onNext(" + i[0] + ") on " + Thread.currentThread().getId()); | |
| subject.onNext(i[0]++); |
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
| Observable.create(o -> { | |
| System.out.println("Created on " + Thread.currentThread().getId()); | |
| o.onNext(1); | |
| o.onNext(2); | |
| o.onCompleted(); | |
| }) | |
| .doOnNext(i -> | |
| System.out.println("Before " + i + " on " + Thread.currentThread().getId())) | |
| .observeOn(Schedulers.newThread()) | |
| .doOnNext(i -> |
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
| Observable.range(0, 5) | |
| .window(3, 1) | |
| .flatMap(o -> o.toList()) | |
| .subscribe(System.out::println); |
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
| Observable.interval(100, TimeUnit.MILLISECONDS) | |
| .take(5) | |
| .window(250, 100, TimeUnit.MILLISECONDS) | |
| .flatMap(o -> o.toList()) | |
| .subscribe(System.out::println); |
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
| Observable<String> left = | |
| Observable.interval(100, TimeUnit.MILLISECONDS) | |
| .map(i -> "L" + i); | |
| Observable<String> right = | |
| Observable.interval(200, TimeUnit.MILLISECONDS) | |
| .map(i -> "R" + i); | |
| left | |
| .join( | |
| right, |
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
| // Produce | |
| Observable<Integer> producer = Observable.create(o -> { | |
| o.onNext(1); | |
| o.onNext(2); | |
| o.onCompleted(); | |
| }); | |
| // Consume | |
| producer.subscribe(i -> { | |
| try { | |
| Thread.sleep(1000); |