Skip to content

Instantly share code, notes, and snippets.

@0ximDigital
Created March 13, 2017 15:21
Show Gist options
  • Save 0ximDigital/55f16b383f6f6b063a98a60a37e5bfc9 to your computer and use it in GitHub Desktop.
Save 0ximDigital/55f16b383f6f6b063a98a60a37e5bfc9 to your computer and use it in GitHub Desktop.
public static void main(String[] args) throws InterruptedException {
Observable.zip(stringObservable().subscribeOn(Schedulers.io()),
integerObservable().subscribeOn(Schedulers.io()),
(s, integer) -> {
timedLog("Got items, zipping");
return s + " " + String.valueOf(integer);
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(zipped -> timedLog("Got zipped value + " + zipped));
Thread.sleep(5000);
}
public static Observable<String> stringObservable() {
return Observable.defer(() -> Observable.fromCallable(Main::getStringItem));
}
public static Observable<Integer> integerObservable() {
return Observable.defer(() -> Observable.fromCallable(Main::getIntegerItem));
}
public static String getStringItem() {
timedLog("Start of getString item");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timedLog("Returning string item");
return "String item";
}
public static Integer getIntegerItem() {
timedLog("Start of getInteger item");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
timedLog("Returning integer item");
return 100;
}
private static void timedLog(final String message) {
final long now = System.currentTimeMillis();
System.out.println(now + " - " + message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment