Module: Jparallel

Defined in:
lib/jparallel.rb

Class Method Summary collapse

Class Method Details

.hashmap(input_hash, poolsize = processor_count, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jparallel.rb', line 23

def self.hashmap (input_hash, poolsize = processor_count, &block)
  output_hash = {}
  thread_pool = Thread.pool poolsize

  input_hash.each_pair do |key, value|
    thread_pool.process do
      output_hash[key] = yield(key, value)
    end
  end

  thread_pool.shutdown
  output_hash
end

.map(input_array, poolsize = processor_count, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jparallel.rb', line 9

def self.map (input_array, poolsize = processor_count, &block)
  output_array = Array.new input_array.length
  thread_pool = Thread.pool poolsize

  input_array.each_with_index do |item, index|
    thread_pool.process do
      output_array[index] = yield(item)
    end
  end

  thread_pool.shutdown # blocks till all the work is done.
  output_array
end

.processor_countObject



5
6
7
# File 'lib/jparallel.rb', line 5

def self.processor_count
  java.lang.Runtime.getRuntime.availableProcessors
end