Particular Thread Count

后端 未结 5 763
花落未央
花落未央 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 01:47

    You can use Thread.enumerate():

    public int countThreadsOfClass(Class clazz) {
        Thread[] tarray = new Thread[Thread.activeCount()];
        Thread.enumerate(tarray);
        int count = 0;
        for(Thread t : tarray) {
            if(clazz.isInstance(t))
                count++;
        }
        return count;
    }
    

提交回复
热议问题