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 

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.