Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created January 8, 2020 04:20
Show Gist options
  • Save qichunren/4e94dcacca7f48daed129a20412fffee to your computer and use it in GitHub Desktop.
Save qichunren/4e94dcacca7f48daed129a20412fffee to your computer and use it in GitHub Desktop.
q = Queue.new
producer = Thread.new {
c = 0
while true do
q << c
c += 1
end
}
printer1 = Thread.new {
while true
val = q.shift
puts "Thread 1: #{val}"
if val > 100
break
end
end
}
printer2 = Thread.new {
while true
val = q.shift
puts "Thread 2: #{val}"
if val > 100
break
end
end
}
producer.join
printer1.join
printer2.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment