Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

How to Keep Track of Number of Threads or Connections

Question:
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.

Answer:
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);
         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 
DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

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