I've been playing around with threads but I keep开发者_StackOverflow running into a problem where the treads seem to just die or stop.
Whats going on here? And how do I get round it?
I've included the code, but didn't paste it here as I think this problem is more fundamental to ruby. source code
thanks.
Edit Ruby 1.8, MacOS (snow leopard)
It looks like you're forgetting to add your new Thread objects to your threads object.
3.times do |t|
threads << Thread.new { word_list.process }
end
Your threads.each {|t| t.join} is working on an empty array, and so is ignoring the threads you did create. Make the change and it should wait.
Edit: I meant to << onto the array, not set it equal.
If you've got threads randomly "going away", make sure you've set Thread.abort_on_exception
to true
. That'll stop the interpreter if an uncaught exception reaches the top-level in a background thread (via SystemExit
, so don't rescue Exception
or they'll get swallowed), and can be immensely useful in tracking down random bugs.
精彩评论