我在玩线程,我发现我不能运行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."发布于 2013-12-15 13:32:10
OS将一个进程可以产生的线程数限制为2046。这也适用于Ruby。
来源和完整说明:"How many threads is too many?"
在OSX上,对一个进程可以产生的线程数量有一个硬性限制。在最近的版本中,的限制大约是2000个线程。
..。
如果我在Linux机器上运行同样的代码,我能够在不眨眼的情况下产生10,000个线程。因此,可能会产生大量线程,但您可能不想这样做。
下面是一个测试程序,您可以运行它来查看限制。
1.upto(10_000) do |i|
Thread.new { sleep }
puts i
endhttps://stackoverflow.com/questions/12074271
复制相似问题