Jump to content

Recommended Posts

Posted

as the title suggest, how can you indicate all the living threads along with a java program when it's being executed??

 

in another word, how can I know how many threads (esp the one implicitly provided by the VM) there are from a running java-desgined program?? is there any class dedicated for this provided from the Java library??

Posted

Well there is a Thread class, and it has various control methods. I don't know if it will fully answer your question, but I would definitely read up on that one.

 

Java is not my strong suit so I'll stop there.

Posted

but Pangloss, the Thread class is only intended to define your own threads,.. the ones invoked by the VM have different implementation.......

 

yet any one??

 

thx

Posted

It seems as long as they don't set themselves into specific Thread Groups that you can use the activeCount() method to find out how many threads there are (as by default they are all put into the first thread group along with the main thread of execution (the one main() etc is running under, the parent etc).

 

This small section of code ive been playing with just now seems to suggest that -

 

/** 
* Class that tests the use of activeCount() across
* multiple threads and whether or not the default
* Thread Group remains constant.
*/
class ThreadTest extends Thread implements Runnable
{
   static int count = 0;
   static int killVar = 0;
   ThreadTest tempThread;
   public void run() {
       waitForDeath();
       System.out.println("WOOOG");
       return;
   }

   public void checkCount () {
       count++;
       if (count < 8) {
           tempThread = new ThreadTest();
           tempThread.start();
           tempThread.checkCount();
       } else {
           System.out.println("Count - " + Integer.toString(count) + " Active Count - " + 
                               Integer.toString(activeCount()));
       }
       return;
   }

   public synchronized void waitForDeath () {
       while (killVar == 0) {
           try {
               wait();
           } catch (InterruptedException e) {
           } 
       }
       if (tempThread != null ) {
           tempThread.kill();
       }
       return;
   }

   public synchronized void kill () {
       killVar = 1;
       notifyAll();
   }

   public static void main ( String Args[] ) {
       ThreadTest testThread = new ThreadTest();
       testThread.start();
       testThread.checkCount();
       System.out.println("Active Count in Main Thread - " + Integer.toString(activeCount()));
       testThread.kill();
   }
}

 

Returns -

 

Z:\>java ThreadTest
Count - 8 Active Count - 9
Active Count in Main Thread - 9
WOOOG
WOOOG
WOOOG
WOOOG
WOOOG
WOOOG
WOOOG
WOOOG

 

So just using activeCount() in the main thread at a certain time will tell you how many threads are running but that will include the main thread and also will include all extended thread objects you have created (as far as i can work out). May want to play around with it a bit or look for an alternative.

Posted

Not a clue :P . Im assuming they're just a way to group threads together nicely and manage them in a certain way. Here is some more info. I'm not really up to date on using threads in Java (or much at all) to any real extent so.... :-( Perhaps if you find some more info you could post it here :)

Posted

alright,

 

1) what is the purpose of creating a thread group object?

 

2) if I dont explicitly unite the created threads along with main thread in a thread group, are they implicitly grouped together??

Posted

Well as far as number (2) goes, yes they seem to be all in the same group by default (that was what the code snippet i posted was for, to check whether this was the case).

 

As for (1), as I said - not a clue other than simple logical grouping of them to aid thought patterns and ease of access. Sorry I can't be more helpful :(.

Posted

any way, even if I dont delve beneath the surface, by now, I know how to detect threads...

 

That's enough....beside curiosity...

 

thx Aet. :D

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.