Last active
January 8, 2016 14:27
-
-
Save DmitriyZaitsev/9e40583a845e0885328d 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
import rx.Observable; | |
import rx.schedulers.Schedulers; | |
import java.util.List; | |
public class SwitchRxSchedulers { | |
public static void main(String[] args) throws InterruptedException { | |
final Object monitor = new Object(); | |
getStrings() | |
.flatMap(Observable::from) | |
.subscribeOn(Schedulers.io()) | |
.doOnNext(Main::printElementAndThread) | |
.map(String::toUpperCase) | |
.observeOn(Schedulers.computation()) | |
.doOnNext(Main::printElementAndThread) | |
.map(String::toLowerCase) | |
.observeOn(Schedulers.newThread()) | |
.subscribe( | |
Main::printElementAndThread, | |
t -> System.err.println(t.getMessage()), | |
() -> { | |
synchronized (monitor) { | |
monitor.notifyAll(); | |
} | |
} | |
); | |
synchronized (monitor) { | |
monitor.wait(); | |
} | |
} | |
private static void printElementAndThread(String s) { | |
System.out.printf("%s : %s%n", s, Thread.currentThread().getName()); | |
} | |
private static Observable<List<String>> getStrings() { | |
return Observable.range(0, 100) | |
.map(i -> String.format("String #%d", i)) | |
.toList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment