Search code examples
kotlinforkjoinpoolkotlin-coroutines

ForkJoinPool and Kotlin Coroutines


As I understand it, by default, if you start a Kotlin Coroutine via launch or async it'll launch in CommonPool (or if you use GlobalScope). And CommonPool is a ForkJoinPool and that, by default, is in non-async mode so it executes tasks in LIFO order. That seems like a very bad choice for something like asynchronous web server applications where we'd want fair scheduling: we don't want the poor sucker who hit our web server first to wait for all calls that came later.

However, Kotlin coroutines add an additional wrinkle here in that there's some bit of code from the Kotlin standard library that will arrange to have those coroutines executed (some variation of the standard asyc select/epoll loop as I understand it). So maybe the LIFO thing isn't a concern?

I could certainly run some experiments and/or step into the code in a debugger to see how this works but I suspect other's have the same question and I bet somebody "just knows" the answer...


Solution

  • Per discussion on Kotlin Discuss CommonPool is no longer the default and they now default to a "mostly fair" scheduler. Details in the linked discussion.