J
James Edward Gray II
Help Thread gurus!
Why is the following code not printing the time every three second the
"Clock" is running, as I believe it should be?
#!/usr/bin/env ruby
require "observer"
require "singleton"
class Clock
include Observable
include Singleton
def start( seconds_delay )
@thread = Thread.new(self, seconds_delay) do |notifier, delay|
loop do
puts "Notifying..."
notifier.notify_observers
sleep delay
end
end
end
def stop
unless @thread.nil?
@thread.kill
@thread = nil
end
end
end
if $0 == __FILE__
class TimePrinter
def update
puts Time.now
end
end
clock = Clock.instance
clock.add_observer TimePrinter.new
puts "Starting clock..."
clock.start 3
sleep 10
clock.stop
puts "Clock stopped..."
sleep 10
end
__END__
Thanks.
James Edward Gray II
Why is the following code not printing the time every three second the
"Clock" is running, as I believe it should be?
#!/usr/bin/env ruby
require "observer"
require "singleton"
class Clock
include Observable
include Singleton
def start( seconds_delay )
@thread = Thread.new(self, seconds_delay) do |notifier, delay|
loop do
puts "Notifying..."
notifier.notify_observers
sleep delay
end
end
end
def stop
unless @thread.nil?
@thread.kill
@thread = nil
end
end
end
if $0 == __FILE__
class TimePrinter
def update
puts Time.now
end
end
clock = Clock.instance
clock.add_observer TimePrinter.new
puts "Starting clock..."
clock.start 3
sleep 10
clock.stop
puts "Clock stopped..."
sleep 10
end
__END__
Thanks.
James Edward Gray II