Particular Thread Count

后端 未结 5 760
花落未央
花落未央 2021-01-24 01:11

I want to know how many active threads are there for a particular Thread class. Lets say I have a class T which extends thread. In some other class (Ex: Demo) , I want to get t

5条回答
  •  情歌与酒
    2021-01-24 02:08

       private static int getThreadCount(){
            int runningThread = 0;
            for (Thread t : Thread.getAllStackTraces().keySet()) if (t.getState()==Thread.State.RUNNABLE && t instanceof YourThreadClassName ) runningThread++;
            System.out.println("Active threads : "+runningThread);
            return runningThread;
        }
    

    Justreplace YourThreadClassName with your class that is extended by Thread.

提交回复
热议问题