Created
February 29, 2012 09:22
-
-
Save iconara/1939396 to your computer and use it in GitHub Desktop.
Simple JRuby worker pool using j.u.c.Executors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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