Skip to content

Instantly share code, notes, and snippets.

@capotej
Last active December 10, 2015 07:18
Show Gist options
  • Save capotej/4400248 to your computer and use it in GitHub Desktop.
Save capotej/4400248 to your computer and use it in GitHub Desktop.
basic skeleton for executing tasks in parallel in jruby
require 'java'
java_import 'java.util.concurrent.ThreadPoolExecutor'
java_import 'java.util.concurrent.TimeUnit'
java_import 'java.util.concurrent.LinkedBlockingQueue'
java_import 'java.util.concurrent.FutureTask'
java_import 'java.util.concurrent.Callable'
java_import 'java.util.concurrent.Executors'
### SETTINGS
concurrency = 80
times = concurrency * 100
### END SETTINGS
executor = Executors.newFixedThreadPool(concurrency)
class ThreadTest
include Callable
def call
sleep 5
print "."
end
end
puts "starting"
tasks = []
times.times { tasks << ThreadTest.new }
executor.invokeAll(tasks)
puts "ending"
executor.shutdown
executor.awaitTermination(2, TimeUnit::MINUTES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment