Why does IO Dispatchers create more than 64 thread in Kotlin corotines?

2.2k views Asked by At

As I understood, Maximum number of threads that can be created by Dispatchers.IO is 64, but my result is a little bit tricky.

this is my code.

repeat(500) {
    CoroutineScope(Dispatchers.IO).launch {
        println(Thread.currentThread().name)
    }
}

and this is the result.

...

DefaultDispatcher-worker-18
DefaultDispatcher-worker-46
DefaultDispatcher-worker-17
DefaultDispatcher-worker-47
DefaultDispatcher-worker-69
DefaultDispatcher-worker-64
DefaultDispatcher-worker-66
DefaultDispatcher-worker-67
DefaultDispatcher-worker-68
DefaultDispatcher-worker-41

...

Why is my threadpool count bigger than 64? Does that mean my actual threads was created more than 64?

thank you for reading! please help me

3

There are 3 answers

3
West_JR On

I don't know how Dispachers.IO names it's threads, but I only get 64 coroutines running at a time.

If you want to limit it to the number of CPUs you have you need to set this system property : kotlinx.coroutines.io.parallelism

fun main(args: Array<String>) {


    class Counter {
        private var current=0
        private var max=0
        fun addOne() {
            synchronized(this) {
                current++
                max = Math.max(max,current)

            }
        }
        fun subOne() {
            synchronized(this) {
                current--
            }
        }
        fun getMax() = max
    }

    val counter=Counter()

    val jobs = (0..500).map {
        CoroutineScope(Dispatchers.IO).launch {
            counter.addOne()
            Thread.sleep(100)
            counter.subOne()
        }
    }
    var done = false
    while(!done) {
        val j = jobs.find{it.isActive}
        if( j == null) {
            done = true
        } else {
            Thread.sleep(100)
        }
    }

    println("max = ${counter.getMax()}")
}
0
Sarah Maher On

Quoting my own article :

Dispatcher.Default has limited to the number of CPU cores while Dispatcher.IO has an elastic thread pool with a configurable max size, who are expected to spend most of their time in a non-runnable state, awaiting the completion of an IO operation.

more can be found here: Kotlin Coroutines for Android - The ABC’s

1
맛난거먹자 On

According to the description of official kotlin manual, more than 64 (default parallelism) threads can be created (but not used) during operations over IO dispatcher because of thread-sharing feature of coroutine.

Please refer to https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html.