How to Keep Track of Number of Threads or Connections

How to Keep Track of Number of Threads or Connections

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.

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes