module Flydata class QueueableThread MAX_JOBS = 60 def initialize(max_jobs = MAX_JOBS) @queue = SizedQueue.new(max_jobs) @stop = false @thread = Thread.new(&method(:run_loop)) @thread.abort_on_exception = true end def run(&block) @queue << block end def join @stop = true @queue << nil if @queue.empty? # wake up the thread @thread.join end private def run_loop until @stop && @queue.empty? block = @queue.shift block.call if block end end end end