Last active
June 14, 2018 00:40
-
-
Save santiagopoli/86aa0477d5a681ef9727eda8fafa8253 to your computer and use it in GitHub Desktop.
SubsribeOnTest
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
import io.reactivex.Observable | |
import io.reactivex.ObservableEmitter | |
import io.vertx.reactivex.core.RxHelper | |
import io.vertx.reactivex.core.Vertx | |
import java.util.concurrent.TimeUnit | |
object Main { | |
val vertx = Vertx.vertx() | |
val blockingScheduler by lazy { vertx.let(RxHelper::blockingScheduler) } | |
val nonBlockingScheduler by lazy { vertx.let(RxHelper::scheduler) } | |
val expensiveComputation = { it: ObservableEmitter<Int> -> | |
printCurrentThread("expensiveComputation") | |
var number = 0 | |
while(!it.isDisposed) { | |
TimeUnit.SECONDS.sleep(1) | |
it.onNext(number++) | |
} | |
} | |
val printCurrentThread = { operationName : String -> | |
println("[$operationName] " + Thread.currentThread().name) | |
} | |
@JvmStatic | |
fun main(args: Array<String>) { | |
Observable.create(expensiveComputation) | |
.observeOn(nonBlockingScheduler) | |
.filter { it % 2 == 0 } | |
.doOnNext { printCurrentThread("onNext afterFilter") } | |
.take(3) | |
.doOnComplete { println("Done") } | |
.subscribeOn(blockingScheduler) | |
.subscribe { println("Received Number: " + it) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: