This is a library for using threads with a similar interface to Java's ExecutorService.
require 'executor'
Create a pre-forked thread pool with 4 threads
pool = Executor::ThreadPool.new(4)
Submit tasks
future = pool.submit do
puts "Working..."
1 + 1
end
Get the result. This blocks until the result is ready
puts future.get
# 2
Shutdown thread pool and wait for completion of all tasks
pool.shutdown
pool.await_termination
Shutdown thread pool immediately and return the list of tasks waiting execution
pool.shutdown!
Create a pre-forked thread pool that executes the given block but with arguments given by calling submit
worker = Executor::Worker(4) do |arg1, arg2|
arg1 + arg2
end
future = worker.submit(10, 20)
puts future.get
# 30
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/executor.
The gem is available as open source under the terms of the MIT License.