Class: Engagement::CommentCounter::Threaded

Inherits:
Object
  • Object
show all
Defined in:
lib/engagement/comment_counter/threaded.rb

Instance Method Summary collapse

Constructor Details

#initialize(places) ⇒ Threaded

Returns a new instance of Threaded.



5
6
7
8
# File 'lib/engagement/comment_counter/threaded.rb', line 5

def initialize(places)
  @places = places
  @mutex = Mutex.new
end

Instance Method Details

#comments_count(url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/engagement/comment_counter/threaded.rb', line 10

def comments_count(url)
  comments_count = 0
  threads = []

  @places.each do |place|
    threads << Thread.new do
      count = place.comments_count(url)
      
      @mutex.synchronize do
        comments_count += count
      end
    end
  end

  threads.each(&:join)
  comments_count
end