Threaded Observer

Discussion in 'Ruby' started by James Edward Gray II, Nov 5, 2004.

  1. Help Thread gurus! :D

    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
    James Edward Gray II, Nov 5, 2004
    #1
    1. Advertising

  2. On Nov 5, 2004, at 5:18 PM, James Edward Gray II wrote:

    > Help Thread gurus! :D


    Figures, I got it as soon as I pushed "Send". The correction is below,
    for the curious.

    Sorry about the noise.

    James Edward Gray II

    > #!/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..."


    changed # I often forget this, for some reason.

    > 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
    >
    >
    >
    James Edward Gray II, Nov 5, 2004
    #2
    1. Advertising

  3. James Edward Gray II

    Guest

    On Sat, 6 Nov 2004, James Edward Gray II wrote:

    > Help Thread gurus! :D
    >
    > 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


    it does for me. on linux. are you on windows? if so the IO (puts) is
    probably causing the whole process to sleep.

    regards.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | When you do something, you should burn yourself completely, like a good
    | bonfire, leaving no trace of yourself. --Shunryu Suzuki
    ===============================================================================
    , Nov 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig
    Replies:
    1
    Views:
    498
    xarax
    Oct 27, 2003
  2. Timo Nentwig
    Replies:
    1
    Views:
    1,175
    Thomas Fritsch
    Apr 16, 2004
  3. Rogan Dawes
    Replies:
    4
    Views:
    783
  4. Steve Green

    Observer methods

    Steve Green, Apr 10, 2005, in forum: Java
    Replies:
    2
    Views:
    393
    Chris Uppal
    Apr 11, 2005
  5. Beatrice Rutger
    Replies:
    0
    Views:
    715
    Beatrice Rutger
    Jun 5, 2005
Loading...

Share This Page