Skip to content

Instantly share code, notes, and snippets.

@schermannj
Forked from DmitriyZaitsev/SwitchRxSchedulers.java
Last active January 8, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schermannj/e13e3d310cf32921b4d6 to your computer and use it in GitHub Desktop.
Save schermannj/e13e3d310cf32921b4d6 to your computer and use it in GitHub Desktop.
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