README

Path: README
Last Update: Sat Feb 11 19:42:41 +0100 2012

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():

  require "threadlimiter"

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

  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():

  require "threadlimiter"

  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

[Validate]