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

gildor

10/09/2018, 5:20 AM
we do not need to create our own threads in Android for any reason?
@Rohit Surwase I don’t see any common reason even without coroutines create own threads on Android if you not a library author (usually you use ThreadPoolExecutor or some high level primitives) All those cases (1-3) are covered easily by coroutines and provide much better API without drawbacks. Especially if 1 and 2 have async api
It’s not so simple question. Just check official kotlinx.coroutines guide, there is a lot of information about threading: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md and also check official guide about UI programming with coroutines: https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md
r

Rohit Surwase

10/09/2018, 6:47 AM
Unfortunately, I have already gone through the docs (1st link) but could not find or understood what I am asking for. I will go through the other link. Thanks so much.
g

gildor

10/09/2018, 6:47 AM
In short: By default coroutines dispatched in own dedicated thread pool with limited capacity that intended to use only with non-blocking code For blocking code use special
<http://Dispatcher.IO|Dispatcher.IO>
that creates new thread (by default up to 64) if you have blocking operation IO and Default dispatchers have different thread limits, but it’s actually more sophisticated, they share single thread pool to be more efficient and share threads. See details here: https://github.com/Kotlin/kotlinx.coroutines/issues/261 For UI/Main thread kotlinx.coroutines provides special dispatchers to dispatch work there (see UI guide)
r

Rohit Surwase

10/09/2018, 6:49 AM
Thanks again.
g

gildor

10/09/2018, 6:50 AM
but could not find or understood what I am asking for
There is you can find description of Default dispatcher and link to documentation: Dispatcher docs: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-dispatchers/-default.html
g

giso

10/10/2018, 6:52 AM
This talk on Kotlinconf helped me to better understand how Coroutines are working:

https://youtu.be/jT2gHPQ4Z1Q

r

Rohit Surwase

10/10/2018, 6:52 AM
Yes, me too.
3 Views