我在想如何知道线程是否正在睡眠。 我搜索了周围,我收集了一些信息,形成这些信息我写了一个方法 isSleeping():布尔,我想我可以放在一个类,以确定线程是否睡觉或不。我只想知道我可能错过了什么。注意:我没有经验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;
}
所以这不会对你没有创建线程......包括主要工作。我不知道有足够多的东西可以轻松地改变JVM在启动时使用它,并通过它创建所有线程创建。此外,您将在中断时永久停留在睡眠状态。 – djechlin 2013-03-13 15:36:47