Does anyone know why GlobalScope Dispatchers.Defau...
# coroutines
s
Does anyone know why GlobalScope Dispatchers.Default contexted coroutines were implemented using Daemon Threads?
1
@Niklas Gürtler.
Copy code
fun coroutinesAreSometimesDispatchedOntoDaemonThreads() = runBlocking<Unit> {
  println("Main. Thread: ${Thread.currentThread()}. isDaemon? ${Thread.currentThread().isDaemon}")

  val scope = CoroutineScope(Dispatchers.Default)
  scope.launch {
    println("Coroutine. Thread: ${Thread.currentThread()}. isDaemon? ${Thread.currentThread().isDaemon}")
  }
}
Prints:
Copy code
Main. Thread: Thread[main,5,main]. isDaemon? false
Coroutine. Thread: Thread[DefaultDispatcher-worker-1,5,main]. isDaemon? true
Is this actually related to
Dispatchers.Default
and only ends up happenign because
GlobalScope
uses that dispatcher when not otherwise specified?
CC @elizarov is there a reason
Dispatchers.Default
coroutines use daemon threads?
n
The Default dispatcher is special, it uses some worker thread... Not sure what the exact purpose is, probably to do long-running calculations or something like that
z
It’s special in that it shares threads with
<http://Dispatchers.IO|Dispatchers.IO>
, not because it uses worker threads. You can create a
CoroutineDispatcher
from any regular java thread pool. As for the purpose of the default dispatcher, I think that’s pretty well-documented in the official kotlin coroutine docs. This question is specifically about why the default dispatcher’s thread pool threads are daemon threads, instead of just regular threads. Daemon threads are a JVM concept, see the java docs for more.