Class: ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



64
65
66
67
68
# File 'lib/thread_pool.rb', line 64

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.



62
63
64
# File 'lib/thread_pool.rb', line 62

def max_size
  @max_size
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/thread_pool.rb', line 74

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

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



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

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

#shutdownObject Also known as: join



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

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

#sizeObject



70
71
72
# File 'lib/thread_pool.rb', line 70

def size
  @workers.size
end