Last active
May 21, 2021 11:37
-
-
Save Jian-Min-Huang/0155309bd8840fa172d70fcd42d44cad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| suspend fun coroutine() = | |
| coroutineScope { | |
| // concurrent call action 1 and 2, cost 200ms | |
| val delay100res = async(Dispatchers.IO) { getAwaitResponse(100) } | |
| val delay200res = async(Dispatchers.IO) { getAwaitResponse(200) } | |
| // blocking call, need return value from action 1 and 2, cost 300ms | |
| val period1 = delay100res.await()["totalTimeMillis"]!! + delay200res.await()["totalTimeMillis"]!! | |
| val delay300res = async(Dispatchers.IO) { getAwaitResponse(period1) } | |
| // concurrent call action 4 and 5, cost 500ms | |
| val period2 = delay300res.await()["totalTimeMillis"]!! | |
| val delay400res = async(Dispatchers.IO) { getAwaitResponse(period2 + 100) } | |
| val delay500res = async(Dispatchers.IO) { getAwaitResponse(period2 + 200) } | |
| mapOf( | |
| "delay100res" to delay100res.await()["totalTimeMillis"]!!, | |
| "delay200res" to delay200res.await()["totalTimeMillis"]!!, | |
| "delay300res" to delay300res.await()["totalTimeMillis"]!!, | |
| "delay400res" to delay400res.await()["totalTimeMillis"]!!, | |
| "delay500res" to delay500res.await()["totalTimeMillis"]!! | |
| ) | |
| } | |
| private suspend fun getAwaitResponse(ms: Long) = webClient.get() | |
| .uri("http://$domain:8888/delay/ms/$ms") | |
| .retrieve() | |
| .awaitBody<Map<String, Long>>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment