Guest User

Untitled

a guest
Oct 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import kotlinx.coroutines.experimental.async
  2. import kotlinx.coroutines.experimental.awaitAll
  3. import kotlinx.coroutines.experimental.delay
  4. import kotlinx.coroutines.experimental.launch
  5. import kotlinx.coroutines.experimental.runBlocking
  6.  
  7. fun main(args: Array<String>) {
  8. runBlocking {
  9. (20..30).forEach {
  10. launch{
  11. println("main before" + it)
  12. val outer = it
  13. delay(1000L)
  14. val lists = (1..10)
  15. .map { async{anotherMethod(outer, it)}}
  16. println("main after------------------Awaiting" + it)
  17. lists.awaitAll()
  18. println("Done awaiting -main after-----------------" + it)
  19.  
  20. }
  21.  
  22.  
  23. }
  24.  
  25. println("Hello,") // main thread continues here immediately
  26. }
  27. }
  28.  
  29. suspend fun anotherMethod (outer: Int,index: Int){
  30. println("inner-b4----" + outer + "--" + index)
  31. delay(3000L)
  32. println("inner-After----" + outer + "--" + index)
  33. }
  34.  
  35. fun main(args: Array<String>) {
  36. runBlocking {
  37. (20..30).forEach {
  38. async{
  39. println("main before" + it)
  40. val outer = it
  41. delay(1000L)
  42. val lists = (1..10)
  43. .map { async{anotherMethod(outer, it)}}
  44. println("main after------------------Awaiting" + it)
  45. lists.awaitAll()
  46. println("Done awaiting -main after-----------------" + it)
  47.  
  48. }
  49.  
  50.  
  51. }
  52.  
  53. println("Hello,") // main thread continues here immediately
  54. }
  55. }
  56.  
  57. suspend fun anotherMethod (outer: Int,index: Int){
  58. println("inner-b4----" + outer + "--" + index)
  59. delay(3000L)
  60. println("inner-After----" + outer + "--" + index)
  61. }
Add Comment
Please, Sign In to add comment