Skip to content

Instantly share code, notes, and snippets.

@evacchi evacchi/Shared.java Secret
Last active Mar 16, 2016

Embed
What would you like to do?
Shared Rx Iterator
public class Shared {
final Observable<Long> doubleFlux = Observable.fromIterable(() -> new Iter()).publish().autoConnect(2);
void stream() {
doubleFlux.to(Publish.Latest()).map(this::fastComputation).subscribe(i -> System.out.println("1) " + i));
doubleFlux.to(Publish.Latest()).map(this::slowComputation).subscribe(i -> System.err.println("2) " + i));
}
public static <T> Function<Observable<T>, Observable<T>> Latest() {
return (flux) -> flux.onBackpressureLatest().subscribeOn(Schedulers.newThread());
}
<T> T fastComputation(T in) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return in;
}
<T> T slowComputation(T in) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return in;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.