简体   繁体   English

Ruby Thread的派生类?

[英]Derived class for Ruby Thread?

I've lived in the C++ world for years, and I'm just starting out with Ruby. 我已经在C ++世界生活多年了,我刚刚开始使用Ruby。 I have a class that I would like to make a thread. 我有一个班级,我想做一个线程。 In Ruby is it wrong to derive a class from Thread? 在Ruby中从Thread派生类是错误的吗? The examples I see use the concept below. 我看到的例子使用了以下概念。

Thread.new { <some block> }

Would it be wrong to do this? 这样做会不对吗?

class MyThread < Thread
  def initialize
  end

  def run
    <main loop>
  end

I think that this is really a question about domain modeling. 我认为这确实是关于域建模的问题。

There would be nothing wrong with what you are doing if you want to extend / enhance the way that a thread behaves - for example to add debug or performance output but I don't think that's what you want. 如果你想扩展/增强线程的行为方式 - 例如添加调试或性能输出,那么你正在做的事情没有任何问题 - 但我认为这不是你想要的。

You probably want to model some concept in your domain with active objects. 您可能希望使用活动对象在域中为某些概念建模。 In that case the standard Ruby approach is better because it allows you to achieve this without bending your domain model. 在这种情况下,标准的Ruby方法更好,因为它允许您在不弯曲域模型的情况下实现此目的。

Inheritance really should only be used to model IS_A relationships. 继承真的应该只用于建模IS_A关系。 The standard ruby code for this neatly wraps up the solution. 这个标准的ruby代码整齐地包含了解决方案。

To make your object active, have it capture the newly created thread in some method 要使对象处于活动状态,请让它以某种方法捕获新创建的线程

Class MyClass

...


   def run
      while work_to_be_done do
         some_work
      end
   end

...

end


threads = []

# start creating active objects by creating an object and assigning
# a thread to each

threads << Thread.new { MyClass.new.run }

threads << Thread.new { MyOtherClass.new.run }

... do more stuff

# now we're done just wait for all objects to finish ....

threads.each { |t| t.join }


# ok, everyone is done, see starships on fire off the shoulder of etc
# time to die ...

That's perfectly fine, I've seen people do that before. 这很好,我见过人们之前就这么做过。 Here's some example code from one of the Ruby mailing lists that runs when Thread.new is called: 以下是调用Thread.new时运行的Ruby邮件列表中的一些示例代码:

class MyThread < Thread
  def initialize
    super("purple monkey dishwasher") {|str| puts "She said, '#{str}.'"}
  end
end

If you plan on calling Thread.fork or Thread.start to run your thread, you should be aware of this from the Ruby documentation those methods : 如果您打算调用Thread.fork或Thread.start来运行您的线程,您应该从Ruby文档中了解这些方法

"Basically the same as Thread::new. However, if class Thread is subclassed, then calling start in that subclass will not invoke the subclass's initialize method." “基本上与Thread :: new相同。但是,如果类是Thread子类,那么在该子类中调用start将不会调用子类的initialize方法。”

I prefer doing the encapsulation thus: 我更喜欢这样封装:

class Threader
  def initialize
    @thread = Thread.new(&method(:thread))
  end

private

  def thread
    # Do thready things...
  end
end

You could also do this directly to a Thread subclass: 您也可以直接对Thread子类执行此操作:

class ThreadyThread < Thread
  def initialize
    super(&method(:thread))
  end

private

  def thread
    # Do thready things...
  end
end

The Thread Ruby Documentation mentions "If the thread is subclassed" so it seems like it should be fine. 线程Ruby文档提到“如果线程是子类”,所以看起来应该没问题。 Make sure if your overwriting initialize, you call super! 确保你的覆盖初始化,你打电话超级!

It's not really the ruby way, but it depends on what you are trying to accomplish with the thread. 它不是真正的红宝石方式,但它取决于你想用线程完成什么。

First off, ruby 1.8 doesn't really have real threads, so they are only really useful for IO-bound things. 首先,ruby 1.8并没有真正的线程,所以它们只对IO绑定的东西非常有用。

Generally in ruby you want something to perform an action in a thread rather than represent a thread, so it is easier to define an ordinary class that creates threads inside to handle the threading aspect. 通常在ruby中,您希望在线程中执行操作而不是表示线程,因此更容易定义在内部创建线程以处理线程方面的普通类。

Inheritance is an IS_A relationship 继承是一种IS_A关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM