How to increase number of threads in CommonPool

Currently, CommonPool only use n-1 ( n = number of cores) threads. How to increase the default number of threads?

You can use newFixedThreadPoolContext to create your own coroutine context, but it is generally bad idea to make thread pool size larger than the number of system processes, it definitively won’t increase performance.

1 Like

Thanks for your anwser, I know it won’t increase performance for CPU-bound tasks. But how about IO-bound tasks?
For example:

    requests.forEach { 
        request ->
        run {
            launch(CommonPool) {
                doRequest(request) //doRequest is a blocking fun and it is from a library so i can't change it anyway.

I think that the whole idea of courtliness is that tasks are not bound to the single thread, so IO performance won’t be affected by thread count (of course you must use non-blocking IO).So in best case, you will have slight decrease in performance due to thread switching, and in worst case, the decrease will be dramatic.


I had the problem that some lib operations block the coroutine. This can even lead to a deadlock if coroutines waiting for each other. My solution was to use the java cached thread pool executor which dynamically adds and removes threads to the pool if needed. Try:


However, this also means that it always provides a thread if needed. So you have to take care to not start too many threads in parallel…

1 Like