Created
March 13, 2017 15:21
-
-
Save 0ximDigital/55f16b383f6f6b063a98a60a37e5bfc9 to your computer and use it in GitHub Desktop.
This file contains 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
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