Class: Future::Threadpool

Inherits:
Object
  • Object
show all
Defined in:
lib/futurevalue/threadpool.rb

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ Threadpool

Returns a new instance of Threadpool.



5
6
7
8
9
# File 'lib/futurevalue/threadpool.rb', line 5

def initialize(count)
  @queue = Queue.new
  @workers = []      
  self.workers = count
end

Instance Method Details

#<<(job) ⇒ Object



23
24
25
# File 'lib/futurevalue/threadpool.rb', line 23

def << (job)
  @queue << job
end

#closeObject



27
28
29
30
# File 'lib/futurevalue/threadpool.rb', line 27

def close
  self.workers = 0
  @workers.each(&:join)
end

#workersObject



11
12
13
# File 'lib/futurevalue/threadpool.rb', line 11

def workers
  @workers.size
end

#workers=(value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/futurevalue/threadpool.rb', line 15

def workers=(value)
  if value > workers
    (value-workers).times { @workers << new_worker }
  else
    (workers-value).times { @queue << proc { @workers.delete(Thread.current); Thread.current.exit } }
  end
end