Class: XThreads::XThread

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &blk) ⇒ XThread

Returns a new instance of XThread.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/xthreads.rb', line 12

def initialize(name, options={}, &blk)
  opts = {interval: 4}.merge(options)
  @name = name
  @initialized = true
  @start = false

  @thread = Thread.new do 
    puts "#{name} created\n"
    Thread.current['name'] = name; 
    loop do
      
      if @start == true then
        blk.call
      else
        puts 'stopped' unless @initialized == true
        @initialized = false
      end 

      Thread.stop if @start == false 

      sleep opts[:interval]
    end
  end

end

Instance Method Details

#killObject



49
50
51
52
# File 'lib/xthreads.rb', line 49

def kill
  puts 'XThread killed'
  @thread.kill
end

#startObject



38
39
40
41
42
# File 'lib/xthreads.rb', line 38

def start
  puts "#{@name} starting ..."
  @start = true
  @thread.run
end

#stopObject



44
45
46
47
# File 'lib/xthreads.rb', line 44

def stop
  puts "'#{@name}' stopping ..."
  @start = false      
end