From: Dale Martenson Date: 2002-04-13T06:39:02+09:00 Subject: RE: confused about threads This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C1E269.87AA8F70 Content-Type: text/plain; charset="iso-8859-1" Mark Probert wrote: > > At 05:58 AM 4/13/2002 +0900, Lyle wrote: > > > I am doing something wrong. Can anyone help me out here? > > > >You never modify the value of 'count' and so you're always passing zero in > >as the first argument of Bob#load. Try replacing this line in the Thread > >block: > > > > b.load(count, out[i]) > > > >with this: > > > > b.load(i, out[i]) > > > >and see if you get the expected result. > > Unfortunately, no. Still empty in the array output department. This seems to work . . . class Bob def load(n,arr) n.times do arr.push(n) end end end count = 10 arr = Array.new out = Hash.new 10.times do |i| out[i] = Array.new arr[i] = Thread.new { sleep(rand(0)/10.0) b = Bob.new #b.load(count,out[i]) b.load(i,out[i]) } end arr.each { |t| t.join } out.keys.each { |v| puts "#{v} --> #{out[v]}" } Note: Passing "i" produces: 8 --> 88888888 4 --> 4444 0 --> 9 --> 999999999 5 --> 55555 1 --> 1 6 --> 666666 2 --> 22 7 --> 7777777 3 --> 333 Note: Setting "count" to a value of 10 produces: 8 --> 10101010101010101010 4 --> 10101010101010101010 0 --> 10101010101010101010 9 --> 10101010101010101010 5 --> 10101010101010101010 1 --> 10101010101010101010 6 --> 10101010101010101010 2 --> 10101010101010101010 7 --> 10101010101010101010 3 --> 10101010101010101010 ------_=_NextPart_001_01C1E269.87AA8F70 Content-Type: text/html; charset="iso-8859-1" RE: confused about threads

Mark Probert wrote:
>
> At 05:58 AM 4/13/2002 +0900, Lyle wrote:
> > > I am doing something wrong.  Can anyone help me out here?
> >
> >You never modify the value of 'count' and so you're always passing zero in
> >as the first argument of Bob#load. Try replacing this line in the Thread
> >block:
> >
> >     b.load(count, out[i])
> >
> >with this:
> >
> >     b.load(i, out[i])
> >
> >and see if you get the expected result.
>
> Unfortunately, no.  Still empty in the array  output department.

This seems to work . . .

class Bob
     def load(n,arr)
       n.times do
         arr.push(n)
       end
     end
end

count = 10
arr = Array.new
out = Hash.new

10.times do |i|
   out[i] = Array.new

   arr[i] = Thread.new {
     sleep(rand(0)/10.0)
     b = Bob.new
     #b.load(count,out[i])
     b.load(i,out[i])
   }
end

arr.each { |t| t.join }
out.keys.each { |v| puts "#{v} --> #{out[v]}" }

Note: Passing "i" produces:

8 --> 88888888
4 --> 4444
0 -->
9 --> 999999999
5 --> 55555
1 --> 1
6 --> 666666
2 --> 22
7 --> 7777777
3 --> 333

Note: Setting "count" to a value of 10 produces:

8 --> 10101010101010101010
4 --> 10101010101010101010
0 --> 10101010101010101010
9 --> 10101010101010101010
5 --> 10101010101010101010
1 --> 10101010101010101010
6 --> 10101010101010101010
2 --> 10101010101010101010
7 --> 10101010101010101010
3 --> 10101010101010101010

------_=_NextPart_001_01C1E269.87AA8F70--