devxlogo

How to Keep Track of Number of Threads or Connections

How to Keep Track of Number of Threads or Connections

Question:
I have a server application listening for clientapplets to connect. The server spawns a new thread each timea connection to a client is established. I want to keeptrack, i.e. count the number of clients connectedand output it to a textfield in a frame. I amnot sure how Java handles shared data among threads so myquestion is: How does one keep track of the number of threadsor connections? I want the number of threads to be displayed ina single frame; I tried something, but a new window is createdeach time a new thread is spawned.

Answer:
Since all threads share the same address space, the simplest solutionis to create a count monitor in the parent thread. Each time theparent thread creates or starts a child thread, it (or the child) incrementsthe count monitor. Each time it stops a thread, it decrements the countmonitor. The count monitor can be passed as a parameter to eachchild thread so it too can increment and decrement it, but in thiscase, access to count needs to be synchronized.

(Note: Java provides a method Thread.activeCount(), whichreturns thenumber of active threads. Unfortunately, this doesn’t get decrementedwhen 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);         child[i].start();      }      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 ... ");      c.updateCount(-1);   }}class Child extends Thread {   private int myID;   private CountMonitor myCount ;   public Child(int i, CountMonitor c) { myID = i; myCount = c;}   public void run() {      myCount.updateCount(1);      for(int i = 0; i < 3; i++) {         System.out.println("   Child-" + myID + ": thread count = " +myCount.getCount());         try { sleep(10); }         catch(InterruptedException e) { System.exit(1); }      System.out.println("   Child-" + myID + " dying ... ");      myCount.updateCount(-1);      }   }} // Child 

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist