在 Ruby 中,有什么区别线程#运行 http://www.ruby-doc.org/core-1.9.3/Thread.html#method-i-run and 主题#wakup http://www.ruby-doc.org/core-1.9.3/Thread.html#method-i-wakeup?
RDoc 指定调度程序不是用 Thread#wakeup 调用的,但这是什么意思?何时使用的示例wakeup vs run?谢谢。
EDIT:
我看到主题#wakup http://www.ruby-doc.org/core-1.9.3/Thread.html#method-i-wakeup导致线程变得可运行,但是如果它不执行直到线程#运行 http://www.ruby-doc.org/core-1.9.3/Thread.html#method-i-run被执行(无论如何都会唤醒线程)?
有人可以提供一个例子吗wakeup做一些有意义的事吗?为了好奇心=)
这是一个例子来说明它的含义(代码示例来自here http://www.java2s.com/Code/Ruby/Threads/Wakeupathread.htm):
线程唤醒
thread = Thread.new do
Thread.stop
puts "Inside the thread block"
end
$ thread
=> #<Thread:0x100394008 sleep>
上面的输出表明新创建的线程由于停止命令而处于休眠状态。
$ thread.wakeup
=> #<Thread:0x100394008 run>
此输出表明线程不再休眠并且可以运行。
$ thread.run
Inside the thread block
=> #<Thread:0x1005d9930 sleep>
现在线程继续执行并打印出字符串。
$ thread.run
ThreadError: killed thread
线程运行
thread = Thread.new do
Thread.stop
puts "Inside the thread block"
end
$ thread
=> #<Thread:0x100394008 sleep>
$ thread.run
Inside the thread block
=> #<Thread:0x1005d9930 sleep>
线程不仅唤醒还继续执行并打印出字符串。
$ thread.run
ThreadError: killed thread
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)