I'm new (very new!) to Ruby but not to programming. I have a
Thread/class inheritance question.I'm trying to make a simple timer
class that extends Thread and repeats a block <count> times and each
repetition separated by <period> seconds. Here's the code
···
--------------
class Timer < Thread
def initialize( period, count, &action )
puts "Begin initialize"
super @period = period @count = count @action = action
puts "End initialize"
end # initialize
def start
puts "Begin start"
1.upto(@count) do
sleep(@period) @action.call
end
puts "End start"
end # start
end #class Timer
threads = []
threads << Timer.new(0.77,3){puts "hi"}
threads.each{ |thr| thr.start}
-------
The output is
Begin initialize
hi
End initialize
Begin start
hi
hi
hi
End start
-------
In my ignorance I expected the supplied block not to be executed in
initialize
and to be executed only in the start method. Please would someone
explain where
I'm going wrong and how to correct it.
In my ignorance I expected the supplied block not to be executed in
initialize
and to be executed only in the start method. Please would someone
explain where
I'm going wrong and how to correct it.
------------------------------------------------------------------------
Creates and runs a new thread to execute the instructions given in
block. Any arguments passed to Thread::new are passed into the
block.
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
x.join # Let the threads finish before
a.join # main thread exits...
produces:
abxyzc
So you see, initialize does run the thread. You might want to write an
independent class that would create the thread in the start function.
Ruby comes with an extensive documentation, at least for standard
classes. I know ri isn't that fast, but faster than any reply here ;-)...
If a block is supplied to Thread.new, this block becomes the thread main loop.
Calling super without parents is equivalent in your case to
super( period, count, &action )
In my ignorance I expected the supplied block not to be executed in
initialize
and to be executed only in the start method. Please would someone
explain where
I'm going wrong and how to correct it.
------------------------------------------------------------------------
Creates and runs a new thread to execute the instructions given in
block. Any arguments passed to Thread::new are passed into the
block.
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
x.join # Let the threads finish before
a.join # main thread exits...
produces:
abxyzc
So you see, initialize does run the thread. You might want to write an
independent class that would create the thread in the start function.
Ruby comes with an extensive documentation, at least for standard
classes. I know ri isn't that fast, but faster than any reply here ;-)...
I'm new (very new!) to Ruby but not to programming. I have a
Thread/class inheritance question.I'm trying to make a simple timer
class that extends Thread and repeats a block <count> times and each
repetition separated by <period> seconds. Here's the code
Are you coming from Java? It sets a bad example for threading. Don't use it
just for conveniences
Here's an example how Ruby does thready things without the risk and
complexity of threads in your own code:
require 'timeout'
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
Look up Timeout, and it might do what you need, or lead to a similar
technique.
In my ignorance I expected the supplied block not to be executed in
initialize
and to be executed only in the start method. Please would someone
explain where
I'm going wrong and how to correct it.
------------------------------------------------------------------------
Creates and runs a new thread to execute the instructions given in
block. Any arguments passed to Thread::new are passed into the
block.
x = Thread.new { sleep 0.1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep 0.2; print "c" }
x.join # Let the threads finish before
a.join # main thread exits...
produces:
abxyzc
So you see, initialize does run the thread. You might want to write an
independent class that would create the thread in the start function.
Ruby comes with an extensive documentation, at least for standard
classes. I know ri isn't that fast, but faster than any reply here ;-)...
I'm new (very new!) to Ruby but not to programming. I have a
Thread/class inheritance question.I'm trying to make a simple timer
class that extends Thread and repeats a block <count> times and each
repetition separated by <period> seconds. Here's the code
Are you coming from Java? It sets a bad example for threading. Don't use
it
just for conveniences
Here's an example how Ruby does thready things without the risk and
complexity of threads in your own code:
require 'timeout'
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
Look up Timeout, and it might do what you need, or lead to a similar
technique.
Thanks for the comments.
I looked at Timeout and, though it could be used here, I think its for
something more imperative than running a block every n seconds and
besides the block might
not terminate before the timeout.
I tried Vincent's idea of having an independant class and setting the
thread in a start method. i.e.
class Timer
def initialize( period, count, &action )
@period = period @count = count @action = action
end #initialize
def start
Thread.new do @count.times do @action.call #sleep(@period)
end #do
end #do
end #start
end #class Timer
timer = Timer.new( 1,5){puts "x"}
timer.start
This works, but if the sleep(@period) is un-commented it only prints "x"
once. If the sleep line is placed before @action.call then "x" is not
printed at all. I've looked at the documentation for sleep but found no
clues.
Ideally what I'd now like to do is make the Timer class such that I
don't need to have a public join method and the @thr.join is, somehow,
called intrinsically.
Ideally what I'd now like to do is make the Timer class such that I
don't need to have a public join method and the @thr.join is, somehow,
called intrinsically.
You do realize, don't you, that if you create a single thread and then join
it, you may as well not have created the thread? Performing a "sleep" call
in the main thread will produce the same result.