调度线程而不停止线程?

发布于 2024-11-27 12:23:46 字数 622 浏览 2 评论 0原文

如果我有类似: 的内容

require 'xmpp4r' # xmpp4r is a ruby library for xmpp. 
  xmppobject.add_meessage_callback { |m|
    # do something with |m|
  }
Thread.stop

,则主线程将进入睡眠状态,然后回调启动的线程才会继续运行。

但是,如果我有这样的东西:

require 'xmpp4r'

  class Foo::App::Bar < Foo::App::Baz

    def method1
      xmppobject.add_message_callback { |m|
        # do something with |m|
      }
    end

end

,属于 Foo::App 的主线程必须始终运行才能使应用程序工作,我无法对其执行 Thread.stop ,因此回调中的线程可以不做它的工作。

这是在 jruby 上,以及我在 irc 上得到的建议:1)使用纤程或 2)条件变量。有人提到纤维不完全适合这里。我刚刚开始使用线程(任何类型),因此将不胜感激有关此的任何建议。

If I have something like:

require 'xmpp4r' # xmpp4r is a ruby library for xmpp. 
  xmppobject.add_meessage_callback { |m|
    # do something with |m|
  }
Thread.stop

, the main thread is put to sleep and only then the thread started by the callback continues running.

But if I have something like this:

require 'xmpp4r'

  class Foo::App::Bar < Foo::App::Baz

    def method1
      xmppobject.add_message_callback { |m|
        # do something with |m|
      }
    end

end

, where the main thread belonging to the Foo::App must be running always for the app to work, I can't do a Thread.stop on it, and hence the thread from the callback can't do its job.

This is on jruby, and the suggestions I got on irc: 1) use fibers or 2)condition variables. Someone mentioned fibers don't exactly fit in here. I have just started with threads ( of any sort ), hence would appreciate any suggestions regarding this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-12-04 12:23:46

事实证明这不是问题。我被 Foo::App 抛出的错误误导了。

Turned out to be a non-issue. I was misled by an error thrown by the Foo::App.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文