Last active
August 29, 2015 14:04
-
-
Save lucasallan/6bf02780ddea0291f0f1 to your computer and use it in GitHub Desktop.
Using Java futures with JRuby
This file contains hidden or 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
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