I will be using thread executors to do some background work with rxkotlin, I made threadpool size fixed to 3, but my problem is during my background operation it using only one thread out of 3, which slows down my background operation
Executor class
class ThreadExe : Executor{
companion object {
private const val THREAD_POOL_SIZE = 3
}
private val executor: Executor =
Executors.newFixedThreadPool(THREAD_POOL_SIZE)
override fun execute(runnable: Runnable) {
executor.execute(runnable)
}
}
The above is my executor class responsible for creating thread.
I will be calling my background task like below
getSomeDataFromNetworkProcessWithDB()
.subscribeOn(Schedulers.from(ThreadExe()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
fun getSomeDataFromNetworkProcessWithDB() {
Observable.fromIteratable(someDataList())
.flatMap {
getSomeNetworkData()
}
.flatMap {
doSomeDbOperation()
}
}
my problem with the above code is all these network and db operation is working sequentially with the same thread, since we have give the threadpool of size 3 it must send the 3 network request parallely, but the request is going sequentially
Can anyone help me out this problem ?
If you want individual operation to run on different thread try this:
getSomeDataFromNetworkProcessWithDB(Schedulers.from(ThreadExe()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
fun getSomeDataFromNetworkProcessWithDB(scheduler: Scheduler): Observable<Data> {
return Observable.fromIterable(someDataList())
.flatMap {
getSomeNetworkData().subscribeOn(scheduler)
}
.flatMap {
doSomeDbOperation().subscribeOn(scheduler)
}
.subscribeOn(scheduler) // optional, if you want fromIterable(), someDataList() to run on this scheduler.
}