• Register
First time here? Checkout the FAQ!
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system assignment writing service bandar bola best waikiki restaurants boiler business car car insurance quotes car insurance quotes car insurance quotes car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cosmetic dentist csr classics hack csr classics hack csr classics hack data data recovery maidenhead deals design development double glazing leeds electrician electrician manchester electricians emergency farm filmy bez limitu filmy online finlock finlock solutions general genral golf holidays spain graduate jobs in london graduate jobs london heap how to jump higher how to jump higher how to jump higher infrared sauna infrared sauna saunas inline skating java judi bola law law firm in leeds lease leasing led lights led bulbs leeds legal smoke life insurance quotes log london manchester mercedes lease deals mercedes leasing mezzanine movies museums and art nikogo szybka chwilówka none none none ny male revue outsource link building paid search agency performance car hire personal personal injury lawyer porcelain veneers ramię szybkie chwilówki recovery restaurant seo seo agencies seo company seo company london seo las vegas seo services services sky diamond seo solutions steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko temp cover car insurance temp cover car insurance temp cover car insurance thread tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web development company window repairs london windows zobaczyć szybka chwilówka

How to Keep Track of Number of Threads or Connections

0 votes
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.
asked Jul 16, 2012 in Java/J2EE by anonymous

1 Answer

0 votes

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 
answered Jul 16, 2012 by sarada Hot Users (2,340 points)