Class: XThreads::XThread
- Inherits:
-
Object
- Object
- XThreads::XThread
- Defined in:
- lib/xthreads.rb
Instance Method Summary collapse
-
#initialize(name, options = {}, &blk) ⇒ XThread
constructor
A new instance of XThread.
- #kill ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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, ={}, &blk) opts = {interval: 4}.merge() @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
#kill ⇒ Object
49 50 51 52 |
# File 'lib/xthreads.rb', line 49 def kill puts 'XThread killed' @thread.kill end |
#start ⇒ Object
38 39 40 41 42 |
# File 'lib/xthreads.rb', line 38 def start puts "#{@name} starting ..." @start = true @thread.run end |
#stop ⇒ Object
44 45 46 47 |
# File 'lib/xthreads.rb', line 44 def stop puts "'#{@name}' stopping ..." @start = false end |