aspose file tools*
The moose likes Threads and Synchronization and the fly likes getting number of threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "getting number of threads" Watch "getting number of threads" New topic
Author

getting number of threads

Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
hi,

i have a multi-threaded program. i want to get the number of threads being used in the program by reading the java file.

can somebody help me on how to do this? the solution may be runtime or compiletime.

any help is appreciated.


cheers,<br />Anandh
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
Not sure I got the question. Can you explain what you mean when you say you want to get the number of threads ... "by reading the java file"?

Do you intend to say you have a java class that spawns several threads and you want to see how many threads have been spawned by that particular class?
Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
Yeah - I have a java program that creates n threads. so, i want to find out how many threads are being created by the program.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And do you want to do this by just reading the .java file? For at least some programs, that's impossible. Or would it be acceptable to run the program, and count how many threads actually exist at a particular time?


"I'm not back." - Bill Harding, Twister
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
You can geta list of all running threads: List Of All Running Threads

I am not sure how you can figure the threads created by a particular code block. I was thinking about stack trace but stack trace is per thread and not across threads. So, it's not possible to know where it was spawned.

If your threads are named (or you could induce code to name them at the point they are created), you could probably iterate through the list of threads to check. But I am not sure you want to/can do that.

If you explain why you need to do this, we may be able to offer an alternative.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting number of threads