Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ruby中找到睡眠线程的时间?_Ruby_Multithreading - Fatal编程技术网

如何在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