我目前正在使用一个程序,该程序利用线程中的回调来操作静态类的类变量。当任何操作对象的方法被调用时,线程就会死掉。如果有人可以看看并告诉我我做错了什么,或者我如何以更好的方式完成同样的事情,将不胜感激。下面是一个复制问题的简单程序。
在 phone.rb 中:
class Phone
@@ringer = "RingVibrateRingVibrateRing"
def self.get_ringer
return @@ringer
end
def set_ringer(new_ring)
@@ringer = new_ring
end
def self.calling_you(reached)
puts "Calling you. "
t = Thread.new {
sleep(3)
reached.call
Thread.exit
}
sleep(10)
puts "Thanks for calling me back!"
end
end
在 CallTree.rb 中
require 'thread'
require 'C:\Users\mabauer\Desktop\phone.rb'
def calling_you_back
ring_sound = Phone.get_ringer
puts "#{ring_sound}"
# The following line is where issues occur, when commented it works fine
Phone.set_ringer("RingingRingingRinging")
ring_sound = Phone.get_ringer
puts "#{ring_sound}"
puts "I called you back."
end
begin
Phone.calling_you(method(:calling_you_back))
end