Class: Threadz::ThreadPool
- Inherits:
-
Object
- Object
- Threadz::ThreadPool
- Defined in:
- lib/threadz/thread_pool.rb
Overview
The ThreadPool class contains all the threads available to whatever context has access to it.
Constant Summary collapse
- KILL_THRESHOLD =
Default setting for kill threshold
10
- THREADS_BUSY_SCORE =
Setting for how much to decrement current kill score by for each queued job
1
- THREADS_IDLE_SCORE =
Setting for how much to increment current kill score by for each idle thread
1
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ ThreadPool
constructor
Creates a new thread pool into which you can queue jobs.
-
#new_batch(opts = {}) ⇒ Object
Return a new batch that’s attached into this thread pool.
-
#process(&block) ⇒ Object
Push a process onto the job queue for the thread pool to pick up.
- #thread_count ⇒ Object
Constructor Details
#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 |
Instance Method Details
#new_batch(opts = {}) ⇒ Object
Return a new batch that’s attached into this thread pool. See Threadz::ThreadPool::Batch for documention on opts.
55 56 57 |
# File 'lib/threadz/thread_pool.rb', line 55 def new_batch(opts={}) return Batch.new(self, opts) end |
#process(&block) ⇒ Object
Push a process onto the job queue for the thread pool to pick up. Note that using this method, you can’t keep track of when the job finishes. If you care about when it finishes, use batches.
48 49 50 51 |
# File 'lib/threadz/thread_pool.rb', line 48 def process(&block) @queue << block nil end |
#thread_count ⇒ Object
41 42 43 |
# File 'lib/threadz/thread_pool.rb', line 41 def thread_count @worker_threads_count.value end |