If you add fmt.Println(runtime.NumCPU()), you'll see there's one CPU in the
playground environment. When main calls sleep after printing 0, the go
routine is ready to run, so it switches there, and prints 1. At that point,
when the go routine sleeps, main is not runnable (it's still sleeping), so
it seems to stay in the context of the go routine, wake up, and print 2,
even though the timer from main expired first. At the next sleep, the main
routine is ready, so it switches there, prints 1, and the process repeats
itself, printing two numbers at a time in each context until they're all
done. If you add a runtime.Gosched() after the sleeps in your go routine,
you'll prompt the scheduler to evaluate, and get the results you expect.
https://play.golang.org/p/P7SBl0xlG8J


On Mon, Apr 8, 2019 at 8:30 AM go je <laurengoc...@gmail.com> wrote:

> https://play.golang.org/p/0TIxVFuqGoB
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to