public
Last active

Simple JRuby worker pool using j.u.c.Executors

  • Download Gist
worker_pool.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# encoding: utf-8
 
require 'java'
 
 
module JUC
java_import 'java.util.concurrent.Executors'
java_import 'java.util.concurrent.TimeUnit'
end
 
class WorkerPool
def initialize(options={})
pool_size = options[:size] || self.class.default_size
@pool = JUC::Executors.new_fixed_thread_pool(pool_size)
end
def self.default_size
java.lang.Runtime.runtime.available_processors
end
def submit(&block)
@pool.submit(block)
end
def shutdown(wait=true)
@pool.shutdown
end
def await_termination(options={})
if options[:poll]
until @pool.await_termination(options[:timeout] || 1, JUC::TimeUnit::SECONDS)
yield if block_given?
end
else
@pool.await_termination(options[:timeout] || 300, JUC::TimeUnit::SECONDS)
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.