Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.coroutines.experimental.async
- import kotlinx.coroutines.experimental.awaitAll
- import kotlinx.coroutines.experimental.delay
- import kotlinx.coroutines.experimental.launch
- import kotlinx.coroutines.experimental.runBlocking
- fun main(args: Array<String>) {
- runBlocking {
- (20..30).forEach {
- launch{
- println("main before" + it)
- val outer = it
- delay(1000L)
- val lists = (1..10)
- .map { async{anotherMethod(outer, it)}}
- println("main after------------------Awaiting" + it)
- lists.awaitAll()
- println("Done awaiting -main after-----------------" + it)
- }
- }
- println("Hello,") // main thread continues here immediately
- }
- }
- suspend fun anotherMethod (outer: Int,index: Int){
- println("inner-b4----" + outer + "--" + index)
- delay(3000L)
- println("inner-After----" + outer + "--" + index)
- }
- fun main(args: Array<String>) {
- runBlocking {
- (20..30).forEach {
- async{
- println("main before" + it)
- val outer = it
- delay(1000L)
- val lists = (1..10)
- .map { async{anotherMethod(outer, it)}}
- println("main after------------------Awaiting" + it)
- lists.awaitAll()
- println("Done awaiting -main after-----------------" + it)
- }
- }
- println("Hello,") // main thread continues here immediately
- }
- }
- suspend fun anotherMethod (outer: Int,index: Int){
- println("inner-b4----" + outer + "--" + index)
- delay(3000L)
- println("inner-After----" + outer + "--" + index)
- }
Add Comment
Please, Sign In to add comment