Class: EnumeratorConcurrent::Threaded

Inherits:
Array
  • Object
show all
Defined in:
lib/enumerator_concurrent/threaded.rb

Overview

Each Iteration in an own Thread. Less overhead then Queued

Instance Method Summary collapse

Methods inherited from Array

#concurrent

Instance Method Details

#each(&block) ⇒ Array

Returns as the standard each does.

Parameters:

  • &block (Block)

    takes a block an passes it to each_to_thread

Returns:

  • (Array)

    as the standard each does



7
8
9
# File 'lib/enumerator_concurrent/threaded.rb', line 7

def each(&block)
  each_to_thread(&block).join_threads
end

#each_to_threadArray<thread>

initializes for every iteration a new Thread

Returns:

  • (Array<thread>)

    Array of Thread instances



19
20
21
# File 'lib/enumerator_concurrent/threaded.rb', line 19

def each_to_thread
  Threaded.new map { |x| Thread.new { yield(x) } }
end

#join_threadsArray<thread.join.value>

joins a list of threads and returns the threads value

Returns:

  • (Array<thread.join.value>)


13
14
15
# File 'lib/enumerator_concurrent/threaded.rb', line 13

def join_threads
  Threaded.new map { |x| x.join.value }
end

#threads(thread_workers) ⇒ Enumerator::ConcurrentQueue

Parameters:

  • thread_workers (Int)

    number of threads running. defaults to four

Returns:

  • (Enumerator::ConcurrentQueue)


25
26
27
# File 'lib/enumerator_concurrent/threaded.rb', line 25

def threads(thread_workers)
  Queued.new self, thread_workers
end