2010/3/9 Jean G. <rubynewbee / gmail.com>:
> Hello,
>
> count =3D 0
> threads =3D []
>
> 10.times do |i|
> =A0threads[i] =3D Thread.new do
> =A0 =A0sleep(rand(0.1))
> =A0 =A0Thread.current["mycount"] =3D count
> =A0 =A0count +=3D 1
> =A0end
> end
>
> threads.each {|t| t.join; print t["mycount"], ", " }
>
>
> For the code above, why the output numbers are random, rather than
> from 0 to 9 by increasing?

Because there are no guarantees about thread scheduling.  Btw, your
code is not really thread safe since you access a shared resource
without proper synchronization (although it might work on some Ruby
platforms).

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/