suresh
02/05/2017, 9:00 PMmakeRequest
is a suspend
function, it blocks that common pool thread until it get a response.
val jobs = (1..1000).map { n ->
defer(CommonPool) {
try {
makeRequest(n)
}catch (e : Exception) {
Pair(n,e.message)
}
}
}
runBlocking {
jobs.forEach {
val r = it.await()
println("Got response for ${r.first} - ${r.second}")
}
}
suspend fun makeRequest(i : Int): Pair<Int, Int> {
println("Creating request $i")
val con = URL("<https://slack.com/>").openConnection() as HttpsURLConnection
con.connectTimeout = 1000
con.readTimeout = 1000
con.instanceFollowRedirects = true
con.requestMethod = "GET"
// sleep(1000)
return Pair(i, con.responseCode)
}