ansman
01/27/2023, 3:33 PMwithContext(Dispatchers.Unconfined) {
println(1)
withContext(Dispatchers.Unconfined) { // Nested unconfined
println(2)
}
println(3)
}
can really print 1, 3, 2
? If the inner withContext
was a launch
I'm fully on board with the fact that the order is undefined. But since withContext
actually returns something it would mean that this code:
withContext(Dispatchers.Unconfined) {
val result = withContext(Dispatchers.Unconfined) { 1 + 1 }
println(result)
}
Could be broken?Sam
01/27/2023, 3:37 PM1, 3, 2
? Have you seen that claim made somewhere? Since there are no calls to launch
or async
, I think your reasoning is correct and this code should execute sequentially.ansman
01/27/2023, 3:37 PMFor example, the following code:
withContext(Dispatchers.Unconfined) {
println(1)
withContext(Dispatchers.Unconfined) { // Nested unconfined
println(2)
}
println(3)
}
println("Done")
Can print both "1 2 3" and "1 3 2", this is an implementation detail that can be changed. But it is guaranteed that "Done" will be printed only when both withContext are completed.
Sam
01/27/2023, 3:40 PMwithContext
works.ansman
01/27/2023, 3:41 PMlaunch
in their exampleNick Allen
01/28/2023, 6:55 AMansman
02/01/2023, 2:40 PMSam
02/01/2023, 2:44 PMwithContext
calls” though.ansman
02/01/2023, 2:46 PM