https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

louiscad

07/13/2018, 12:42 PM
Hi, do Android people there know if it's possible to make a Handler run in CommonPool?
d

dalexander

07/13/2018, 1:09 PM
Doing a quick check of the documentation, it looks like in theory it's possible, but probably complicated and a bad idea. Handlers generally need a Looper to operate on. Since CommonPool represents a group of Threads, that means you'll have to initialize a Looper for each Thread (and you might want a custom LooperPool subclass, which I don't know if it exists, to do the dispatching). You would then be able to create a Handler on the Looper, but since Handlers aren't coroutine aware there's a risk that you end up just blocking the Thread the Handler is running on.
m

mcastiblanco

07/13/2018, 6:32 PM
I don't think it would be safe/reliable because
Handler
is tied to a specific thread and the Kotlin runtime will decide which thread from the pool to use, which can change each time the suspending computation is resumed.
I guess using
Handlers
+ coroutines would need a single thread context to be safe
but let us know your experience if you try to use it with
CommonPool
🤣
3 Views