Jowel Tisso
Jowel Tisso

Reputation: 57

Running 10 kotlin async coroutines concurrently

I am trying to run 7 async coroutines concurrently but only 4 async coroutines seems to be running

 btn.setOnClickListener(View.OnClickListener {

        var urls = arrayOf(mUrl,mUrl2,mUrl3,mUrl4,mUrl5,mUrl5,mUrl5)

        GlobalScope.launch(Dispatchers.Default) {

            for(url in urls){
                async { getlocationSourcecode(url) }  // This is jsoup parsing html method 
            }
            Log.i("MainActivity", " TEST : This Main coroutine is running in ${Thread.currentThread().name} thread ")
        }

    })

This is my log where you can see only 4 threads are working

   2020-05-02 16:52:15.177 11578-11578/com.example.android.iscrape I/MainActivity:  TEST : This Main thread is running in main thread 
2020-05-02 16:52:20.759 11578-11674/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-2 thread 
2020-05-02 16:52:20.762 11578-11676/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-4 thread 
2020-05-02 16:52:20.763 11578-11675/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-3 thread 
2020-05-02 16:52:20.765 11578-11673/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-1 thread 
2020-05-02 16:52:22.977 11578-11674/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-2 thread 
2020-05-02 16:52:23.761 11578-11674/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-2 thread 
2020-05-02 16:52:24.393 11578-11674/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-2 thread 
2020-05-02 16:53:43.252 11578-11675/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-3 thread 
2020-05-02 16:55:22.039 11578-11673/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-1 thread 
2020-05-02 16:55:22.453 11578-11673/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-1 thread 
2020-05-02 16:55:22.923 11578-11673/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-1 thread 
2020-05-02 16:55:23.397 11578-11675/com.example.android.iscrape I/MainActivity:  TEST : This asyncTask thread is running in DefaultDispatcher-worker-3 thread 
2020-05-02 16:55:23.863 11578-11675/com.example.android.iscrape I/MainActivity:  TEST : This Main coroutine is running in DefaultDispatcher-worker-3 thread

This is the initial part of the jsoup method with also parses json
Here i have logged the thread name to check how many threads are running

suspend fun getlocationSourcecode(url: String) {

    lateinit var trying: String

    Log.i("MainActivity", "TEST : getDatax  called ")
    Log.i("MainActivity", " TEST : This asyncTask thread is running in ${Thread.currentThread().name} thread ")

Upvotes: 2

Views: 981

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

You are using Dispatchers.Default as the coroutine dispatcher. Quoting the documentation:

It is backed by a shared pool of threads on JVM. By default, the maximum number of threads used by this dispatcher is equal to the number of CPU cores, but is at least two.

Based on your Logcat output, it would appear that you are running on a quad-core CPU, as there appears to be four threads in the Dispatchers.Default thread pool.

You are welcome to use Executors.newThreadPoolExecutor() to create a thread pool with more threads on it, then use the asCoroutineDispatcher() extension function on Executor to wrap it in a CoroutineDispatcher for you to use instead of Dispatchers.Default. However, unless your work is I/O bound, that is probably unnecessary on a mobile device. And, for I/O bound work, you should be considering Dispatchers.IO, which shares threads with Dispatchers.Default but also uses an auxiliary thread pool with many more threads in it.

Upvotes: 4

Related Questions