Does anyone know why GlobalScope Dispatchers.Defau...
# coroutines
Does anyone know why GlobalScope Dispatchers.Default contexted coroutines were implemented using Daemon Threads?
@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}")
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
and only ends up happenign because
uses that dispatcher when not otherwise specified?
CC @elizarov is there a reason
coroutines use daemon threads?
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
It’s special in that it shares threads with
, not because it uses worker threads. You can create a
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.