スレッドで遊んでいて、10000スレッドを実行できないことがわかりました。
次のエラーが発生します。
threading.rb:23:in `initialize': can't create Thread (35) (ThreadError)
from threading.rb:23:in `new'
from threading.rb:23:in `block in <main>'
from threading.rb:22:in `times'
from threading.rb:22:in `<main>'
次に、最大数を確認しようとしました。最大2046スレッドを作成すると、Rubyがコードを実行します。
なぜ2046年なのか?512、1024、2046などのメモリパターンに従っているようです...
threading.rbコード:
threads = []
counter = 1000
ARGV.each do |a|
counter = a.to_i
end
lines = 0
counter.times do |i|
puts "This is index number #{i}."
end
puts "You've just seen the normal printing and serial programming.\n\n"
counter.times do |i|
Thread.new do
some_number = Random.rand(counter)
sleep 1
puts "I'm thread number #{i}. My random number is #{some_number}.\n"
lines += 1
end
end
messaged = false
while lines < counter
puts "\nWaiting to finish.\n" unless messaged
print '.'
puts "\n" if lines == counter
messaged = true
end
puts "\nI've printed #{lines} lines.\n"
puts "This is end of the program."