Skip to content

Instantly share code, notes, and snippets.

@streetsmartdev
Created October 20, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streetsmartdev/c89bfb7321dc6b2e66c7cb37f7b39ab2 to your computer and use it in GitHub Desktop.
Save streetsmartdev/c89bfb7321dc6b2e66c7cb37f7b39ab2 to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val methodTime = measureTimeMillis {
val apiResult = runBlocking(Dispatchers.Default) {
val asyncResult1 = async { callApi() }
val asyncResult2 = async { callApi() }
val asyncResult3 = async { callApi() }
listOf(asyncResult1, asyncResult2, asyncResult3).awaitAll()
}
println(apiResult)
}
println("Time: ${methodTime}ms")
}
fun callApi(): String {
val client = OkHttpClient()
val okRequest = Request.Builder()
.url("https://httpbin.org/get")
.build()
val responseResource: Response = client.newCall(okRequest).execute()
return responseResource.use {
responseResource.body?.string()!!
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment