Created
October 20, 2019 09:38
-
-
Save streetsmartdev/c89bfb7321dc6b2e66c7cb37f7b39ab2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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