Class: ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/tpkg/thread_pool.rb

Defined Under Namespace

Classes: Worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 10) ⇒ ThreadPool

Returns a new instance of ThreadPool.



81
82
83
84
85
# File 'lib/tpkg/thread_pool.rb', line 81

def initialize(max_size = 10)
  @max_size = max_size
  @queue = Queue.new
  @workers = []
end

Instance Attribute Details

#max_sizeObject

Returns the value of attribute max_size.



79
80
81
# File 'lib/tpkg/thread_pool.rb', line 79

def max_size
  @max_size
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/tpkg/thread_pool.rb', line 91

def busy?
  @queue.size < @workers.size
end

#process(block = nil, &blk) ⇒ Object



102
103
104
105
106
# File 'lib/tpkg/thread_pool.rb', line 102

def process(block=nil,&blk)
  block = blk if block_given?
  worker = get_worker
  worker.set_block(block)
end

#shutdownObject Also known as: join



95
96
97
98
# File 'lib/tpkg/thread_pool.rb', line 95

def shutdown
  @workers.each { |w| w.stop }
  @workers = []
end

#sizeObject



87
88
89
# File 'lib/tpkg/thread_pool.rb', line 87

def size
  @workers.size
end