在看《Programming Ruby》第二版。 运行书中的例子:
class Chaser
attr_reader :count
def initialize(name)
@name = name
@count = 0
end
def chase(other)
while @count < 5
while @count - other.count > 1
Thread.pass
end
@count += 1
print "#@name : #{count}\n"
end
end
end
c1 = Chaser.new("A")
c2 = Chaser.new("B")
threads = [
Thread.new { Thread.stop ; c1.chase(c2) },
Thread.new { Thread.stop ; c2.chase(c1) }
]
start_index = rand(2)
threads[start_index].run
threads[1-start_index].run
threads.each { |t| t.join }
运行时报这样的错:
chaser.rb:35:in `join': No live threads left. Deadlock? (fatal)
from chaser.rb:35:in `block in <main>'
from chaser.rb:35:in `each'
from chaser.rb:35:in `<main>'
Ruby 版本:ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
Ruby 新手,不知道如何处理