Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting number of threads

 
Anandh Ramesh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic