• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting number of threads

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic