This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which threads are still alive  RSS feed

 
Phil Hopgood
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning all,

In a multi-threaded application, is there any way of discovering/listing which threads (thread names) are still "alive", i.e. have not yet finished their run() method? I mean programmatically not using some monitor.

I know I can find out how many are active using Thread.activeCount() but is there an array or something lying around that holds the names for those threads?

I don't mean all the threads associated with the JVM's process of running the application, i.e. garbage collection, etc. just the ones related to the application, main, thread1, thread2, etc.

I'm guessing that there's no way of finding out the status of the active threads since by the time you've got that the status will probably have changed anyway.

Thanks for all your help, really appreciate it.
Regards,
Phil.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil:
Morning all,


I think ranch must specify a variable in the editor that changes with the time zones. As it is afternoon here in India
Anyways, coming to serious matters.

Phil:
In a multi-threaded application, is there any way of discovering/listing which threads (thread names) are still "alive", i.e. have not yet finished their run() method? I mean programmatically not using some monitor.


Well yes you can find out.
Thread.enumerate(Thread[] tarray) is the method that does it for you.
But that method will list only the threads that are a part of the current thread thread group.
If you need to find threads for your application, you can make sure that all your threads have a known thread group.

Phil:
I'm guessing that there's no way of finding out the status of the active threads since by the time you've got that the status will probably have changed anyway.


Well, i would say that there is a way but the information retrieved may be outdated by the time you read it.
[ September 10, 2008: Message edited by: Nitesh Kant ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One option is to use JMX - the JConsole tool will show all threads.

If you want to do it programmatically, check out the code I posted in this thread.
 
Phil Hopgood
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, Nitesh,

Thanks for that guys.

More stuff to play with

Regards,
Phil
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!