我想知道如何确定一个线程是否正在睡眠。 我搜索了一些信息,从这些信息中,我编写了一个方法isSleeping():boolean,我认为可以将其放在一个类中来确定线程是否正在睡眠。我只是想知道我可能错过了什么。注意:我没有经验,零天经验。
//isSleeping returns true if this thread is sleeping and false otherwise.
public boolean isSleeping(){
boolean state = false;
StackTraceElement[] threadsStackTrace = this.getStackTrace();
if(threadsStackTrace.length==0){
state = false;
}
if(threadsStackTrace[0].getClassName().equals("java.lang.Thread")&&
threadsStackTrace[0].getMethodName().equals("Sleep")){
state = true;
}
return state;
}