Thread#run和Thread#wakeup之间的区别? [英] Difference between Thread#run and Thread#wakeup?
问题描述
在Ruby中, Thread#run 有什么区别和线程#wakup ?
In Ruby, what is the difference between Thread#run and Thread#wakup?
RDoc指定不使用Thread#wakeup调用 scheduler ,但这是什么意思?何时使用唤醒与 run 的示例?谢谢.
The RDoc specifies that scheduler is not invoked with Thread#wakeup, but what does that mean? An example of when to use wakeup vs run? Thanks.
我看到 Thread#wakup 导致线程变得可运行,但是如果要在 Thread#运行(是否仍会唤醒线程)被执行?
I see that Thread#wakup causes the thread to become runnable, but what use is it if the it's not going to execute until Thread#run is executed (which wakes up the thread anyway)?
有人可以提供一个示例,其中唤醒会产生有意义的作用吗?出于好奇的缘故=)
Could someone please provide an example where wakeup does something meaningful? For curiosity's sake =)
推荐答案
Here is an example to illustrate what it means (Code example from here):
Thread.wakeup
thread = Thread.new do
Thread.stop
puts "Inside the thread block"
end
$ thread
=> #<Thread:0x100394008 sleep>
以上输出表明由于停止命令,新创建的线程处于睡眠状态.
The above output indicates that the newly created thread is asleep because of the stop command.
$ thread.wakeup
=> #<Thread:0x100394008 run>
此输出表明线程不再处于休眠状态,并且可以运行.
This output indicates that the thread is not sleeping any more, and can run.
$ thread.run
Inside the thread block
=> #<Thread:0x1005d9930 sleep>
现在,线程继续执行并打印出字符串.
Now the thread continues the execution and prints out the string.
$ thread.run
ThreadError: killed thread
Thread.run
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>
线程不仅会唤醒,还会继续执行,并打印出字符串.
The thread not only wakes up but also continues the execution and prints out the string.
$ thread.run
ThreadError: killed thread
这篇关于Thread#run和Thread#wakeup之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!