Skip to content

Instantly share code, notes, and snippets.

@amanshuraikwar
Created December 30, 2018 15:50
Show Gist options
  • Save amanshuraikwar/2a9c3b53e8077ade1ef9a5e89bd78072 to your computer and use it in GitHub Desktop.
Save amanshuraikwar/2a9c3b53e8077ade1ef9a5e89bd78072 to your computer and use it in GitHub Desktop.
RxJava Thread Switching like a Pro - Medium Article - 2
/*
* RxJava simple demonstration of subscribeOn() and observeOn()
*/
println("Calling function : Thread = ${Thread.currentThread().id}")
Observable
.just(1)
.doOnNext {
println("OnNext : Val = $it : Thread = ${Thread.currentThread().id}")
}
.map {
println("Map : Val = $it : Thread = ${Thread.currentThread().id}")
2
}
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.newThread())
.subscribe(
{
// onNext
println("OnNext : Val = $it : Thread = ${Thread.currentThread().id}")
},
{
// onError
},
{
// onComplete
println("OnComplete : Thread = ${Thread.currentThread().id}")
}
)
/*
* End
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment