Hi, I have a small question just to be safe: ``` ...
# reaktive
m
Hi, I have a small question just to be safe:
Copy code
val singleScheduler = createSingleScheduler()
        singleScheduler.newExecutor().submit {
            // do something
        }
Does everytime I do
newExecutor().submit
here ensure it will run on the same thread like in java concurrent when we use
Executors.newSingleThreadExecutor()
?
a
Hey, yes every SingleScheduler has its own thread shared between Executors. Don't forget to dispose Executors.
m
Thanks, what are the best practices here for disposing executor? Should I dispose inside the lamda, something like this?
Copy code
val singleScheduler = createSingleScheduler()
        val executor = singleScheduler.newExecutor()
        singleScheduler.newExecutor().submit {
            // do something
            // At the end dispose
            executor.dispose()
        }
a
Yes it's fine. Also you need to dispose schedules if they are not needed anymore to avoid thread leaking. There is built-in shared SingleScheduler available via
singleScheduler
top level property. This lives in process scope and does not require disposal. Also if you just need to execute a task in background you can use the following:
completableFromFunction { ... }.subscribeOn(scheduler).subscribe()
. It will return Disposable for cancellation of needed.
m
Cool, thanks 🙂