Handling arrays in threads

Hi all.
I need to adding elements to an array, which is inside another array.
suppose: $arr = [[],[],[]] or simply: $arr = Array.new(3,[])
@n = 1
from the main thread the “$arr[@n] << ‘e’” method populate all the first
level items of the array: [[‘e’],[‘e’],[‘e’]].
from the main thread the “$arr[@n] += [‘e’]” method works well and do
what i need: [[‘e’],[‘e’,‘e’],[‘e’]].
but inside the thread the “$arr[@n] += [‘f’]” method replace all the
other items of the sub-array: [[‘e’],[‘f’],[‘e’]].

here the code:

module JavaLang
include_package “java.lang”
end

class needed for threading the fetching phase.

class OnlineFetch
include JavaLang::Runnable
def initialize(host,br,ln,h)
@host = host
@br = br
@ln = ln
@h = h
end

def run
begin
puts “in #{@host} thread.”
puts $found.inspect # for debug purposes
$found[@ln] << @br.method(@host).call(@h)
puts $found.inspect # for debug purposes
puts “#{@host} => #{$found[@ln].last}”
rescue Timeout::Error => e
puts “#{@host} does not respond”
end
end
end

The Output is that:

Fetching Value 1
/usr/share/jruby/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/module.rb:35
warning: replacing Thread with Java::JavaLang::Thread in constant
'Thread on class/module JavaLang
in 0 thread.
in 1 thread.[[], [], []]

[[], [], []]
in 2 thread.
[[], [], []]
in 3 thread.
[[], [], []]
[[nil], [], []]
2 =>
[[""], [], []]
3 =>
[[nil], [], []]
0 =>
[[""], [], []]
1 =>
Fetching Value 2
in 0 thread.
in 1 thread.[[""], [], []]

[[""], [], []]
in 2 thread.
[[""], [], []]
in 3 thread.
[[""], [], []]
[[""], [nil], []]
0 =>
[[""], [“found!”], []]
3 => found!
[[""], [nil], []]
2 =>
[[""], [nil], []]
1 =>
Fetching Value 3
in 0 thread.
[[""], [nil], []]
in 1 thread.
[[""], [nil], []]
in 2 thread.
in 3 thread.
[[""], [nil], []][[""], [nil], []]

[[""], [nil], [“found!”]]
1 => found!
[[""], [nil], [nil]]
2 =>
[[""], [nil], [“found!”]]
3 => found!
[[""], [nil], [nil]]
0 =>
[[""], [nil], [nil]]

as you can see the found values aren’t add to the sub-array but replace
the current value.

here are some tests that i had done with jirb.

irb(main):001:0> $found = [[],[]]
=> [[], []]
irb(main):002:0> $found[0] += [“non”]
=> [“non”]
irb(main):003:0> $found[0] += [“e”]
=> [“non”, “e”]
irb(main):004:0> $found[0] += [“un”]
=> [“non”, “e”, “un”]
irb(main):005:0> $found[0] += [“gioco”]
=> [“non”, “e”, “un”, “gioco”]
irb(main):006:0> $found
=> [[“non”, “e”, “un”, “gioco”], []]
irb(main):007:0> $found[0] += [nil]
=> [“non”, “e”, “un”, “gioco”, nil]
irb(main):008:0> $found
=> [[“non”, “e”, “un”, “gioco”, nil], []]
irb(main):009:0> $found[0] << [nil]
=> [“non”, “e”, “un”, “gioco”, nil, [nil]]
irb(main):010:0> $found[0] << “azzo”
=> [“non”, “e”, “un”, “gioco”, nil, [nil], “azzo”]
irb(main):011:0> $found
=> [[“non”, “e”, “un”, “gioco”, nil, [nil], “azzo”], []]
irb(main):012:0> quit

Sorry for my bad English.

Hi Massimo,

On Sep 1, 2011, at 5:50 AM, Massimo D. wrote:

Hi all.
I need to adding elements to an array, which is inside another array.
suppose: $arr = [[],[],[]] or simply: $arr = Array.new(3,[])

These two constructs are not equivalent. [[], [], []] creates an array
containing three different empty arrays. Array.new(3, []) creates an
array containing three copies of the same empty array.

You don’t show the full script that produced the output you provided,
but I wonder if this misconception is fueling your problem.

Rhett

Rhett S. wrote in post #1019595:

These two constructs are not equivalent. [[], [], []] creates an array
containing three different empty arrays. Array.new(3, []) creates an
array containing three copies of the same empty array.
Awesome!
Thanks! that solved the problem.