我正在尝试同时运行7个异步协程,但似乎只有4个异步协程正在运行

 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 ")
        }

    })

这是我的日志,您只能看到4个线程在工作
   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

这是jsoup方法的初始部分,还解析json

我在这里记录了线程名称,以检查正在运行的线程数
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 ")

最佳答案

您正在使用Dispatchers.Default作为协程调度程序。引用the documentation:



根据Logcat的输出,您似乎正在四核CPU上运行,因为Dispatchers.Default线程池中似乎有四个线程。

欢迎您使用Executors.newThreadPoolExecutor()创建一个具有更多线程的线程池,然后使用asCoroutineDispatcher()上的Executor扩展功能将其包装在CoroutineDispatcher中供您使用,以代替Dispatchers.Default使用。但是,除非您的工作受I / O约束,否则在移动设备上可能没有必要。并且,对于I / O绑定(bind)工作,您应该考虑使用 Dispatchers.IO ,它与Dispatchers.Default共享线程,但还使用其中包含更多线程的辅助线程池。

10-05 17:48