ThreadLimiter forks threads like Thread.fork(), but limits the number of concurrently running threads.

ThreadLimiter isn’t a thread pool. Each fork really starts a new thread.

Example: Get the titles of a large collections of URL’s.

The traditional way, using Thread directly:

urls           = [.....]                 # A lot of URL's. Maybe even thousends.

titles =
urls.collect do |url|
  Thread.fork do
    # ... get the title of the url...
  end
end.collect do |thread|
  thread.value
end

With ThreadLimiter#fork():

thread_limiter = ThreadLimiter.new(10)   # Max. 10 concurrently running threads.
urls           = [.....]                 # A lot of URL's. Maybe even thousends.

titles =
urls.collect do |url|
  thread_limiter.fork do
    # ... get the title of the url...
  end
end.collect do |thread|
  thread.value
end

With Enumerable#threaded_collect():

urls           = [.....]                 # A lot of URL's. Maybe even thousends.

titles =
urls.threaded_collect(10) do |url|       # Max. 10 concurrently running threads.
  # ... get the title of the url...
end