How to check if a thread is sleeping?

后端 未结 5 498
旧时难觅i
旧时难觅i 2020-12-17 17:13

Is there any way to check if a given thread is sleeping?

5条回答
  •  失恋的感觉
    2020-12-17 17:44

    Here is an fairly ugly hack to check if the other thread is sleeping:

    public static boolean isSleeping(Thread t) {
        StackTraceElement[] ste = t.getStackTrace();
    
        if (ste.length == 0)
            return false;     // thread has terminated!
    
        return ste[0].getClassName().equals("java.lang.Thread")
            && ste[0].getMethodName().equals("sleep");
    }
    

提交回复
热议问题