Forked from DmitriyZaitsev/SwitchRxSchedulers.java
Last active
January 8, 2016 14:28
-
-
Save schermannj/e13e3d310cf32921b4d6 to your computer and use it in GitHub Desktop.
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
package com.schrmnnj.sample | |
import rx.Observable | |
import rx.schedulers.Schedulers | |
/** | |
* Created on 08.01.16. | |
*/ | |
class RxMultiThreadCheck { | |
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") | |
fun check(): Unit { | |
val monitor: Object = Object(); | |
getStrings() | |
.flatMap({ Observable.from(it) }) | |
.subscribeOn(Schedulers.io()) | |
.doOnNext({ printElementAndThread(it) }) | |
.map({ it.toUpperCase() }) | |
.observeOn(Schedulers.computation()) | |
.doOnNext({ printElementAndThread(it) }) | |
.map({ it.toLowerCase() }) | |
.observeOn(Schedulers.newThread()) | |
.subscribe({ | |
printElementAndThread(it) | |
}, { | |
println(it.message) | |
}, { | |
synchronized (monitor) { | |
monitor.notifyAll(); | |
} | |
}) | |
synchronized (monitor) { | |
monitor.wait(); | |
} | |
} | |
companion object { | |
fun getStrings(): Observable<List<String>> { | |
return Observable.range(0, 100).map({ "String $it" }).toList(); | |
} | |
fun printElementAndThread(s: String): Unit { | |
println("$s : ${Thread.currentThread().name}"); | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
RxMultiThreadCheck().check() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment