调度线程而不停止线程?
如果我有类似: 的内容
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明这不是问题。我被 Foo::App 抛出的错误误导了。
Turned out to be a non-issue. I was misled by an error thrown by the Foo::App.