Skip to content

IO Dispatcher creates more than 64 threads #2272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
unnamedw opened this issue Sep 26, 2020 · 1 comment
Closed

IO Dispatcher creates more than 64 threads #2272

unnamedw opened this issue Sep 26, 2020 · 1 comment
Labels
docs KDoc and API reference question

Comments

@unnamedw
Copy link

unnamedw commented Sep 26, 2020

Annotation describes IO Dispatcher like this,

It defaults to the limit of 64 threads or the number of cores (whichever is larger).

As I understood, Maximum number of threads that can be created by Dispatchers.IO is up to 64 because I use only 6core and 12 threads CPU, 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 threadpool count bigger than 64?
Does that mean actual threads was created more than 64?

@unnamedw unnamedw changed the title Why does IO Dispatchers create more than 64 thread in Kotlin corotines? Why does IO Dispatchers create more than 64 thread? Sep 26, 2020
@unnamedw unnamedw changed the title Why does IO Dispatchers create more than 64 thread? IO Dispatchers create more than 64 thread Sep 26, 2020
@unnamedw unnamedw changed the title IO Dispatchers create more than 64 thread IO Dispatcher creates more than 64 thread Sep 26, 2020
@unnamedw unnamedw changed the title IO Dispatcher creates more than 64 thread IO Dispatcher creates more than 64 threads Sep 26, 2020
@qwwdfsad qwwdfsad added question docs KDoc and API reference labels Sep 28, 2020
@qwwdfsad
Copy link
Collaborator

Thanks for pointing it out, I'll rephrase the doc a bit.

Dispatchers.IO shares underlying threadpool with Dispatchers.Default, so the number of simultaneous threads is 64 + #CORES_SIZE. In your particular example, underlying threadpool starts compensating all 64 IO threads with a few threads for potential tasks in Dispatchers.Default

qwwdfsad added a commit that referenced this issue Oct 7, 2020
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference question
Projects
None yet
Development

No branches or pull requests

2 participants