The program compiles fine. And when I run it, I got following error:
I'm guessing the problem is that the goroutines are blocking on the
sleep call. When a goroutine blocks, the thread it's running on cannot
run other goroutines (they're moved to other threads). So, while you
can practically create thousands of goroutines, you can only have n
goroutines blocking at the same time (where n is the maximum number of
threads your operating system can create for a process).
It should, but it doesn't right now.
More runtime work is needed.
Russ
as a work around, if you use time.After (or time.NewTimer) you should
avoid this problem - only one goroutine will call Sleep at a time.
Threads and goroutines are N:M; a thread can have multiple goroutines,
and a goroutine can move between different threads. When a goroutine
blocks on a syscall (other kinds of blocking, such as channel
operations, don't cause this AFAIK), the other goroutines on the same
thread are moved to other threads.
time.Sleep currently calls sleep syscall every time it's called, but
it can be implemented without it. time.After uses such trick.
syscalls have to block the whole thread.
That's just what they do - your operating
system kernel is not aware of Go.
The runtime takes care of making sure that
if a thread gets blocked by one goroutine,
the other goroutines run in some other thread.
Russ
-rob