Skip to content

Instantly share code, notes, and snippets.

@lucasallan
Last active August 29, 2015 14:04
Show Gist options
  • Save lucasallan/6bf02780ddea0291f0f1 to your computer and use it in GitHub Desktop.
Save lucasallan/6bf02780ddea0291f0f1 to your computer and use it in GitHub Desktop.
Using Java futures with JRuby
require 'java'
java_import 'java.util.concurrent.FutureTask'
java_import 'java.util.concurrent.Executors'
class JavaFuture
class Callable
def initialize(block)
@block = block
end
def call
@block.call
return true
end
end
def initialize(&block)
@executor = Executors.new_cached_thread_pool
@task = Callable.new(block)
end
def executor
@executor
end
def execute
@executor.submit(FutureTask.new(@task))
end
end
f = JavaFuture.new { puts "Hello"; sleep(10); puts 'Again' }
f.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment