saplingPro
saplingPro

Reputation: 21329

Knowing the number of threads started by a particular thread

In the following program main thread calls startThread which gets started on a new thread.It calls greet_thread which in turn calls greet on a new thread. startThread calls greet_thread till the count is less than or equal to 10.

Is there any way I can tell how many threads are currently running ? Being more specific, I want to know the number of threads currently running started by a call to greet_thread. As greet_thread is called 10 times,it is obvious that 10 threads will be running alone at the end. But is there a way to know the number ?

This is hierarchy of the threads started in the program :

main_thread
  | 
 \ /
starts a new thread by calling startThread
  |
 \ /
startThread starts a new thread by calling greet_thread-->--|
  |                                                         |
 \ /                                                       \ / gets called 10 times
 greetThread is started with an infinite loop------<------- |
  |
 \ /
 greet() method is called

class Tester {

    private static  int count = 0;

    public static void main(String args[]) {
        startThread();
    }

    public static void startThread() {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                while(count <= 10) {
                    greet_thread(count);
                    count++;
                }
            }
        };
        new Thread(r).start();
    }

    public static void greet_thread(final int count) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                while(true) {
                    greet();
                }
            }
        };
        new Thread(r).start();
    }

    public static void greet() {
        System.out.println("GREET !");
    }
}

Upvotes: 3

Views: 182

Answers (2)

Stephen C
Stephen C

Reputation: 718788

If you simply want to count the number of threads that are still alive, then change the run method to increment a shared counter when it starts, and decrement it when it terminates (normally or via an exception).

Note you will need to use something like an AtomicInteger to implement the counter ... or do something to synchronize the updates. Incrementing a primitive integer is not atomic, and this can lead to heisenbugs if not adequately synchronized.

(And the same for your existing count variable!)

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

If you have 30K threads running you have a problem. I suspect you don't have this many CPUs. You can check the number of threads using VisualVM, or a ThreadGroup or a thread pool, or using a counter.

Usually when you design a program you know how many threads you want and you only need to check this is the case. You don't deliberately write a program with an unknown, but very high number of threads and try to work out what it is later because this number won't be very useful.

Upvotes: 3

Related Questions