Home > Mobile >  Usage example when Thread is preferred over Coroutine in Kotlin
Usage example when Thread is preferred over Coroutine in Kotlin

Time:01-16

I'm researching concurrency topic in Kotlin, and can't figure out why do we still need a Threads, if Coroutines are such a good tool to handle concurrency in Kotlin.

I've tried to find some benefits of Threads usage in Kotlin, but all I can see is that how good Coroutines are.

CodePudding user response:

Sometimes you need something more straightforward, even if it's less performant and, as mentioned in a comment by @broot, if you are writing in Java it will probably be a lot easier to use a library that doesn't expose suspend functions.

Also, threads even being more expensive are a lot more accurate when handling delays. An example would be choreographing an animation, if you try to do it with delay(randomDelay) function you will get gaps between animation changes as the coroutine might continue several milliseconds after randomDelay has passed.

Other than that, use coroutines when possible.

  • Related