Concurrency Coordination Reports
Sized Queue Capacity Coordination Report
Offer items into a bounded sized queue with nonblocking pushes and report the remaining capacity as open, tight, or full.
Sized Queue Capacity Coordination Report
sized_queue_capacity_coordination_report.rb
offered =
capacity = 2
buffer = SizedQueue.new(capacity)
accepted = 0
rejected = 0
offered.times do |item|
begin
buffer.push(item, true)
accepted += 1
rescue ThreadError
rejected += 1
end
end
remaining = capacity - buffer.size
status = if rejected > 0
"full"
elsif remaining == 0
"tight"
else
"open"
end
puts "offered=#{offered}"
puts "accepted=#{accepted}"
puts "remaining=#{remaining}"
puts "status=#{status}"
offered =
capacity = 2
buffer = SizedQueue.new(capacity)
accepted = 0
rejected = 0
offered.times do |item|
begin
buffer.push(item, true)
accepted += 1
rescue ThreadError
rejected += 1
end
end
remaining = capacity - buffer.size
status = if rejected > 0
"full"
elsif remaining == 0
"tight"
else
"open"
end
puts "offered=#{offered}"
puts "accepted=#{accepted}"
puts "remaining=#{remaining}"
puts "status=#{status}"
offered =
capacity = 2
buffer = SizedQueue.new(capacity)
accepted = 0
rejected = 0
offered.times do |item|
begin
buffer.push(item, true)
accepted += 1
rescue ThreadError
rejected += 1
end
end
remaining = capacity - buffer.size
status = if rejected > 0
"full"
elsif remaining == 0
"tight"
else
"open"
end
puts "offered=#{offered}"
puts "accepted=#{accepted}"
puts "remaining=#{remaining}"
puts "status=#{status}"
bounded capacity
A `SizedQueue` has fixed capacity, so `push(item, true)` buffers an item when there is room and raises `ThreadError` when the queue is full. The accepted and rejected counts describe how much shared capacity remains for producers.