Re: Subclassing Thread?



Stephen Ware wrote:

Those familiar with threading in Java will know that you can write a
class that is a subclass of Thread, and then put the thread's code into
'public static void run,' which gets executed once thread.start is
called. Can something similar to this be done in Ruby?


The following simulates calling run to start a thread. It stops the
thread as soon as it starts, and then restarts the thread when needed:

class MyThread < Thread
def initialize(x, y)
super(x, y) do |val1, val2|
Thread.stop
puts "In thread..."
puts val1, val2
puts
end
end
end

t = MyThread.new("hello", "world")
puts "main program"
puts
sleep(2)

t.run

t.join
sleep(2)
puts "main program ending..."





--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • Re: object specific methods and id
    ... Your ObjectSpace example is a good illustration of the singleton class/singleton object relationship models a class/instance relationship without actually being a class/instance relationship. ... as if S were a subclass of A but... ... puts 'This is the foo method printing' ...
    (comp.lang.ruby)
  • REXML inheritance confusion (simple q?)
    ... So I created my own subclass of REXML::Document. ... class MyDoc < REXML::Document ... puts @tree.class ...
    (comp.lang.ruby)
  • Re: Reading a class-file and calling it at runtime.
    ... extremely confusing circular inheritance: ... This might be a clearer diagram: ... Object (Object is a subclass of Class) ... puts Class.kind_of? ...
    (comp.lang.ruby)