Method: Threadz::ThreadPool#initialize

Defined in:
lib/threadz/thread_pool.rb

#initialize(opts = {}) ⇒ ThreadPool

Creates a new thread pool into which you can queue jobs. There are a number of options:

:initial_size

The number of threads you start out with initially. Also, the minimum number of threads. By default, this is 10.

:maximum_size

The highest number of threads that can be allocated. By default, this is the minimum size x 5.

:kill_threshold

Constant that determines when new threads are needed or when threads can be killed off. If the internally tracked kill score falls to positive kill_threshold, then a thread is killed off and the kill score is reset. If the kill score rises to negative kill_threshold, then a new thread is created and the kill score is reset. Every 0.1 seconds, the state of all threads in the pool is checked. If there is more than one idle thread (and we’re above minimum size), the kill score is incremented by THREADS_IDLE_SCORE for each idle thread. If there are no idle threads (and we’re below maximum size) the kill score is decremented by THREADS_KILL_SCORE for each queued job.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/threadz/thread_pool.rb', line 27

def initialize(opts={})
  @min_size = opts[:initial_size] || 10 # documented
  @max_size = opts[:maximum_size] || @min_size * 5 # documented

  # This is our main queue for jobs
  @queue = Queue.new
  @worker_threads_count = AtomicInteger.new(0)
  @min_size.times { spawn_thread }
  @killscore = 0
  @killthreshold = opts[:kill_threshold] || KILL_THRESHOLD # documented

  spawn_watch_thread
end