Skip to content

Instantly share code, notes, and snippets.

@cheald
Forked from pmahoney/executor.rb
Last active August 29, 2015 14:25
Show Gist options
  • Save cheald/eea7cbbcc2b90c81f243 to your computer and use it in GitHub Desktop.
Save cheald/eea7cbbcc2b90c81f243 to your computer and use it in GitHub Desktop.
require 'java'
class Task
include Java::JavaUtilConcurrent::Callable
def initialize(num)
@num = num
end
def call
puts "This is task #{@num}"
sleep 0.2
@num
end
end
Executors = Java::JavaUtilConcurrent::Executors
executor = Executors.newFixedThreadPool(8)
futures = (1..100).map do |i|
executor.submit Task.new(i)
end
rets = futures.map { |f| f.get }
puts "return values: #{rets.join(',')}"
executor.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment