在线程中调用相同的过程不起作用
Same procedure doesn't work called in a Thread
我在 Ruby 中遇到线程问题。
这是我第一次使用 Threads,所以可能我省略了一些重要的东西。
我有这个程序:
def define_action(field)
false if field.nil? || field.empty?
puts field.empty? <---- ONLY FOR DEBUG
case field['special']
when 'signpost'
typewriter_animation(field['message'])
else
typewriter_animation("TO DO ... add action for #{field['special']}")
end
end
typewriter_animation
清理字段,输入文本并等待 5 秒,然后再次清除字段。
我在线程中使用 define_action
@timer = Thread.new do
@engine.define_action(@map.next_field_coordinates)
end
当 field
为 empty
时(我在提示符中看到 true
)我除了结果什么都没有,但程序继续并在屏幕上打印 "TO DO ... add action for"
没有 Thread 的相同代码可以完美运行,但显然会停止屏幕 5 秒。
我的代码有什么问题?
您的保护子句缺少 return
。
def define_action(field)
false if field.nil? || field.empty? # this line does nothing
...
end
大概应该是:
def define_action(field)
return false if field.nil? || field.empty?
...
end
我看不出线程会有什么不同。
我在 Ruby 中遇到线程问题。 这是我第一次使用 Threads,所以可能我省略了一些重要的东西。
我有这个程序:
def define_action(field)
false if field.nil? || field.empty?
puts field.empty? <---- ONLY FOR DEBUG
case field['special']
when 'signpost'
typewriter_animation(field['message'])
else
typewriter_animation("TO DO ... add action for #{field['special']}")
end
end
typewriter_animation
清理字段,输入文本并等待 5 秒,然后再次清除字段。
我在线程中使用 define_action
@timer = Thread.new do
@engine.define_action(@map.next_field_coordinates)
end
当 field
为 empty
时(我在提示符中看到 true
)我除了结果什么都没有,但程序继续并在屏幕上打印 "TO DO ... add action for"
没有 Thread 的相同代码可以完美运行,但显然会停止屏幕 5 秒。
我的代码有什么问题?
您的保护子句缺少 return
。
def define_action(field)
false if field.nil? || field.empty? # this line does nothing
...
end
大概应该是:
def define_action(field)
return false if field.nil? || field.empty?
...
end
我看不出线程会有什么不同。