Hi, do Android people there know if it's possible ...
# coroutines
l
Hi, do Android people there know if it's possible to make a Handler run in CommonPool?
d
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
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
🤣