複数のスレッドで数値を計算するスクリプトを作成したいと思います。各スレッドは2の累乗を計算しますが、最初のスレッドは2から計算を開始し、2番目は4から、3番目は8から計算を開始し、間にテキストを出力する必要があります。
例:
Im a thread and these are my results
2
4
8
Im a thread and these are my results
4
8
16
Im a thread and these are my results
8
16
32
私の失敗コード:
def loopa(s)
3.times do
puts s
s=s**2
end
end
threads=[]
num=2
until num == 8 do
threads << Thread.new{ loopa(num) }
num=num**2
end
threads.each { |x| puts "Im a thread and these are my results" ; x.join }
私の失敗の結果:
Im a thread and these are my results
8
64
4096
8
64
4096
8
64
4096
Im a thread and these are my results
Im a thread and these are my results