假設我有一個類Talker
。我使用一個隊列來讓發言者說話,但我偶爾想要讓發言者靜音,但是當我取消發言者的靜音時,我希望發言人在他離開的地方繼續發言。如何阻止線程從隊列中獲取消息,並等待我取消靜音以恢復發言者的身份?從Ruby中的處理隊列中暫停線程
class Talker
def initialize
@queue = Queue.new
@threads = Array.new(1) do
Thread.new do
until @queue.empty?
# what logic should go here to check if mute
# and stop taking messages?
next_msg = @queue.shift
puts next_msg
end
end
end
end
def speak(msg)
@queue.push(msg)
end
# stop threads from taking messages from queue
def mute
# what goes here?
end
# allow threads to continuing taking messages from queue
def unmute
# what goes here?
end
end