Vlad
07/28/2020, 12:32 PMsuspend fun networkCall(): Int {
delay(100)
return Random.nextInt(0,100)
}
GlobalScope.launch {
val s1 = async { networkCall() }
val s2 = async { networkCall() }
val s3 = async { networkCall() }
val s4 = async { networkCall() }
println("${s1.await()} ${s2.await()} ${s3.await()} ${s4.await()}")
}
output is always same of one value doesn't matter what network call returns
7 7 7 7
But this one is fine
GlobalScope.launch {
val s1 = async { networkCall() }
val s2 = async { networkCall() }
val s3 = async { networkCall() }
val s4 = async { networkCall() }
val s1a = s1.await()
val s2a = s2.await()
val s3a = s3.await()
val s4a = s4.await()
println("$s1a $s2a $s3a $s4a")
}
First one works fine on JVMgildor
07/28/2020, 12:45 PMgildor
07/28/2020, 12:45 PMaraqnid
07/28/2020, 12:54 PMprintln("${s1.await()} ${s2.await()} ${s3.await()} ${s4.await()}")
println("results: ${setOf(s1.await(), s2.await(), s3.await(), s4.await())}")
and the first line looks wrong (all same) but the second line looks rightaraqnid
07/28/2020, 12:55 PMaraqnid
07/28/2020, 12:56 PMprintln(this.result_0.toString() + ' ' + this.result_0 + ' ' + this.result_0 + ' ' + this.result_0);
this looks like the culpritaraqnid
07/28/2020, 12:56 PMcase 2:
this.state_0 = 3;
this.result_0 = this.local$s2.await(this);
if (this.result_0 === COROUTINE_SUSPENDED)
return COROUTINE_SUSPENDED;
continue;
case 3:
this.state_0 = 4;
this.result_0 = this.local$s3.await(this);
if (this.result_0 === COROUTINE_SUSPENDED)
return COROUTINE_SUSPENDED;
continue;
case 4:
this.state_0 = 5;
this.result_0 = this.local$s4.await(this);
if (this.result_0 === COROUTINE_SUSPENDED)
return COROUTINE_SUSPENDED;
continue;
case 5:
println(this.result_0.toString() + ' ' + this.result_0 + ' ' + this.result_0 + ' ' + this.result_0);
araqnid
07/28/2020, 12:57 PMaraqnid
07/28/2020, 12:57 PMIvan Kubyshkin [JetBrains]
07/28/2020, 1:40 PMgildor
07/28/2020, 3:46 PMRob Murdock
07/28/2020, 4:33 PMRob Murdock
07/28/2020, 4:34 PMaraqnid
07/28/2020, 5:33 PMaraqnid
07/28/2020, 5:42 PM