[ACCEPTED]-How to check if a thread is sleeping?-sleep
You can call Thread.getState()
on and check if the state 3 is TIMED_WAITING
.
Note, however that TIMED_WAITING
doesn't necessarily 2 mean that the thread called sleep()
, it could also 1 be waiting in a Object.wait(long)
call or something similar.
Here is an ugly hack to check if the other 1 thread is sleeping (calling Thread.sleep
):
public static boolean isSleeping(Thread t) {
StackTraceElement[] ste = t.getStackTrace();
return ste.length > 0
&& ste[0].getClassName().equals("java.lang.Thread")
&& ste[0].getMethodName().equals("sleep");
}
I am not sure if there is a better way but 3 you could change a variable when a thread 2 goes to sleep and check that variable if 1 the thread is sleeping or not.
You could create your own sleep method which 4 records the Thread's ID to a global variable and use it as reference 3 for sleeping thread.
There's no other way 2 you can tell if a thread is precisely sleeping.
Hope 1 the three links below help:
i didn't actually did it , but there's an 3 ThreadMXBean Interface for getting thread Info
which returns 2 ThreadInfo Class, there you might get something with getWaitedTime 1 method.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.