Andrea Giuliano
12/12/2020, 5:31 PMAndrea Giuliano
12/12/2020, 5:39 PMyield()
with delay(input.toLong() * 1000)
then I can see them running on complete different threads.
Now I also understand that the null is given by the fact that the coroutine is running on a thread after it has completed, so probably the thread local gets emptied. Maybe Thread.sleep() is taking the thread and not allowing a coroutine to be dispatched on it?Tijl
12/12/2020, 8:15 PMAndrea Giuliano
12/12/2020, 8:49 PMTijl
12/12/2020, 9:30 PMsuspend
point (like yield
can execution switch to another part of your code (which also has to be suspend
). Thread.sleep
is just a JRE method which is unaware of any of this.Andrea Giuliano
12/12/2020, 9:53 PMTijl
12/12/2020, 10:08 PMjoinAll
but since this is for coroutines that do practically nothing that is a very small window, further more since you use DEFAULT
scheduling in your launch, the coroutines you launched in that context are already scheduled.
there is a small chance for coroutines launched by other context (which you yourself schedule in other threads) to steal some time during joinAll
but the chance of that is very minimal, probably they just spawn too late and you’re already at print
the reason you get null
in thread 2 is because nothing is ever set there.Tijl
12/12/2020, 10:15 PMExecutors.newSingleThreadExecutor().asCoroutineDispatcher()
, launching something on it, set your thread local and delay
, then launch something else a little later (but less than the delay) on the same dispatcher, and set the thread local to something else. then after the delay the value will have changed.Tijl
12/12/2020, 10:16 PMTijl
12/12/2020, 10:25 PMTijl
12/12/2020, 10:26 PMAndrea Giuliano
12/13/2020, 11:59 AM