Skip to content

Instantly share code, notes, and snippets.

@mrjabba
Created July 11, 2012 20:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjabba/3093156 to your computer and use it in GitHub Desktop.
Save mrjabba/3093156 to your computer and use it in GitHub Desktop.
how to schedule a newScheduledThreadPool in jruby
require 'java'
java_import 'java.util.concurrent.Executors'
java_import 'java.util.concurrent.TimeUnit'
class SomeClass
include java.lang.Runnable
def run
puts "imma lil jruby runner"
end
end
class Scheduler
def schedule
puts "starting..."
service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(SomeClass.new, 4, 2, TimeUnit::SECONDS);
puts "i can haz service? #{service.class}"
puts "done..."
end
end
Scheduler.new.schedule
@davetron5000
Copy link

Depending on what's in your real SomeClass, you might want to schedule a shutdown:

Signal.trap("SIGINT") do # or whatever signals end your app
  scheduler.shutdown
  scheduler.await_termination(2,TimeUnit::SECONDS)
end

This will tell the schedule not to start any new threads, and the second call will wait 2 seconds for any threads that are running to shut down. You could also do shutdown_now to force an exit (but calling exit would likely do that, too :)

@mrjabba
Copy link
Author

mrjabba commented Jul 11, 2012

Thanks Dave! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment