如何在ruby中找到睡眠线程的时间?
我的工作线程内有恒定的循环。我想检查线程睡眠的时间 如何找到线程的睡眠时间 这是密码如何在ruby中找到睡眠线程的时间?,ruby,multithreading,Ruby,Multithreading,我的工作线程内有恒定的循环。我想检查线程睡眠的时间 如何找到线程的睡眠时间 这是密码 t = Thread.new{ loop do puts "thread " threads=Thread.list threads.each_with_index do |th,index| th.status == "run" ? th.priority = 2 : th.priority =
t = Thread.new{
loop do
puts "thread "
threads=Thread.list
threads.each_with_index do |th,index|
th.status == "run" ? th.priority = 2 : th.priority = 0
if th.status == "false" || th.status == "sleep"
th.kill
end
print "thread #{th} : State=> #{th.status} : priority=> #{th.priority} :index=> #{index} \n "
end
end
}
我想知道睡眠线程的时间,这样我就可以杀死正确的线程。由于线程的生命周期应该在某个阶段终止您可以设置线程级别变量,以在创建给定线程时获取信息:
t = Thread.new{}
t[:created_at] = Time.now
若你们想知道线程睡眠的时间有多长,你们需要在每次线程进入睡眠状态时设置这个值,并在它醒来时重置它。
所以你的支票看起来像
th.kill if Time.now - th[:created_at] > MAX_THREAD_LIFETIME