I am working on thread which has constant loop inside. I want to check for how long the thread is sleeping.
How can I find the sleeping time of the thread?
Here is the Code
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
}
I want to know the time of the sleeping thread so that i can kill the right one . As life of thread should be killed at some stage
You can set thread level variable to get info when given thread was created:
If you want to know for how long thread is sleeping, you need to set that value every time thread go to sleep and reset it once it wakes up.
So your check would look like