Skip to content

Instantly share code, notes, and snippets.

@iconara
Created February 29, 2012 09:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconara/1939396 to your computer and use it in GitHub Desktop.
Save iconara/1939396 to your computer and use it in GitHub Desktop.
Simple JRuby worker pool using j.u.c.Executors
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment