我想知道我怎么知道线程是否在睡觉。我四处搜索并收集了一些信息,从这些信息中我写了一个方法 isSleeping():boolean ,我认为我可以将其放入一个类中以确定线程是否处于睡眠状态。我只想知道我可能错过了什么。注意:我没有经历过0天的经验。
//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;
}