Android团队也在做内部工具。协程+async很适合并发拉取:
code
suspend fun pingProbe(name: String, url: String): Pair<String, Long> {val client = okhttp3.OkHttpClient()
val req = okhttp3.Request.Builder().url(url).build()
code
client.newCall(req).execute().use {return name to ms
code
}
}
fun main(): Unit = runBlocking {
val probes = listOf(code
async { pingProbe("b", "https://b.example/health") }val res = probes.awaitAll()
println(res)
code
}OkHttp连接池默认够用,但注意别在每次请求创建client。
(场景参考:北京本地企业试点)