spring_boot which should I choose? `Thread()` vs...
# announcements
m
spring_boot which should I choose?
Thread()
vs Kotlin coroutines?
a
Coroutines uses reusable threads, and are for way more things than just threading. As such you can await for the result, do inter communication, and also things like deep recursion (storing stack items in heap using that). According to your workflow, if you have alot of things going on again and again, creating a
Thread
may cause slowdown as it needs ~500KB of memory to be allocated as stack, and everytime it destructs it needs to free it down. Whereas if you have things like spawned thread, blocks forever till request is received or so, and then do some work, or even if it is not regularly created, it may be better, because you free up the threads automatically as they're completed.
m
so in other words, I should use coroutines whenever possible, right?
a
I mean if you want to have Java/JS interop, or if you have work that doesn't need quickly creating/spawning of multiple threads, then native
Threads
are all ok, because that's what being used behing the coroutines for them to run on, but they attach a event loop which make them sleep when not in use and reuse them.
For small works, coroutines are cheap, for one-time things threads may be cheap.
👍 1
m
ok thank you for the info