Java Data Type Tutorial - Java ThreadGroup.activeCount()








Syntax

ThreadGroup.activeCount() has the following syntax.

public int activeCount()

Example

In the following code shows how to use ThreadGroup.activeCount() method.

public class Main {
// w  w w.  java 2  s. c  om
  public static void main(String[] args) {
    ThreadGroupDemo tg = new ThreadGroupDemo();
  }
}

class ThreadGroupDemo implements Runnable {
  public ThreadGroupDemo() {

    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    System.out.println("Active threads in \"" + pGroup.getName() + "\" = "
        + pGroup.activeCount());

  }

  public void run() {

    System.out.println(Thread.currentThread().getName()
        + " finished executing.");
  }
}

The code above generates the following result.