Class: DLogReader::SimpleThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_logreader/distributer/simple_thread_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker, num_threads = 5) ⇒ SimpleThreadPool

Returns a new instance of SimpleThreadPool.



6
7
8
9
10
11
12
13
14
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 6

def initialize(worker, num_threads = 5)
  self.worker = worker
  self.num_threads = num_threads
  self.queue = Queue.new
  self.max_queue_size = 100
  num_threads.times do 
    create_thread
  end
end

Instance Attribute Details

#max_queue_sizeObject

Returns the value of attribute max_queue_size.



5
6
7
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 5

def max_queue_size
  @max_queue_size
end

#num_threadsObject

Returns the value of attribute num_threads.



5
6
7
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 5

def num_threads
  @num_threads
end

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 5

def queue
  @queue
end

#thread_poolObject

Returns the value of attribute thread_pool.



5
6
7
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 5

def thread_pool
  @thread_pool
end

#workerObject

Returns the value of attribute worker.



5
6
7
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 5

def worker
  @worker
end

Instance Method Details

#joinObject



23
24
25
26
27
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 23

def join
  while(queue.size > 0)
    sleep 0.1
  end
end

#process(line) ⇒ Object



16
17
18
19
20
21
# File 'lib/distributed_logreader/distributer/simple_thread_pool.rb', line 16

def process(line)
  while(queue.size > self.max_queue_size)
    sleep(0.01)
  end
  self.queue << line
end