RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 20, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How to Keep Track of Number of Threads or Connections

I have a server application listening for client applets to connect. The server spawns a new thread each time a connection to a client is established. I want to keep track, i.e. count the number of clients connected and output it to a textfield in a frame. I am not sure how Java handles shared data among threads so my question is: How does one keep track of the number of threads or connections? I want the number of threads to be displayed in a single frame; I tried something, but a new window is created each time a new thread is spawned.

Since all threads share the same address space, the simplest solution is to create a count monitor in the parent thread. Each time the parent thread creates or starts a child thread, it (or the child) increments the count monitor. Each time it stops a thread, it decrements the count monitor. The count monitor can be passed as a parameter to each child thread so it too can increment and decrement it, but in this case, access to count needs to be synchronized.

(Note: Java provides a method Thread.activeCount(), which returns the number of active threads. Unfortunately, this doesn't get decremented when threads are stopped.)


class CountMonitor {

   private  int count = 0; // = # of active threads

   public synchronized void updateCount(int amt) {

      // don't decrement a zero count
      if (count <= 0 && amt <= 0) {
         try { wait(); }
         catch(InterruptedException e) {};

      count += amt;
      notify(); // start waiting threads

   public int getCount() { return count; }


public class Parent {

   public static void main(String args[]) {

      CountMonitor c = new CountMonitor();

      int N = 5;

      c.updateCount(1);  // for parent

      Child child[] = new Child[N];

      // start some threads
      for(int i = 0; i < N; i++) {
         child[i] = new Child(i, c);

      for(int i = 0; i < N; i++) {
         System.out.println("Parent: thread count = " + c.getCount());
         try { Thread.sleep(10); }
         catch(InterruptedException e) { System.exit(1); }

      System.out.println("Parent dying ... ");


class Child extends Thread {

   private int myID;
   private CountMonitor myCount ;

   public Child(int i, CountMonitor c) { myID = i; myCount = c;}

   public void run() {
      for(int i = 0; i < 3; i++) {
         System.out.println("   Child-" + myID + ": thread count = " +
         try { sleep(10); }
         catch(InterruptedException e) { System.exit(1); }

      System.out.println("   Child-" + myID + " dying ... ");
} // Child 
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date