0

In a java application, is there a way to check if a thread is still running from a different place than where the thread has started?

I have a certain thread running somewhere in java application. How can I check the status of that thread pool. I have the name for that certain thread.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
ankit jain
  • 350
  • 2
  • 12

3 Answers3

2

yourThread.isAlive() returns if the thread is alive and has not yet died.

Also yourThread.getState()gives you the state of the thread.

'...if the thread has stopped for some reason, I need to perform actions based on that result...' so your thread stops for some reason with nothing abnormal happens and if it was an exception you could catch it in try-catch block:

try{
}
catch(Exception e){
}

There might be an error or something else. Try to use this to catch it:

try {
    // your tasks
} catch (Throwable e) {
   // ...I need to perform actions based on that result...
}

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. ...

An instance of ThreadDeath is thrown in the victim thread when the (deprecated) Thread.stop() method is invoked.

The top-level error handler does not print out a message if ThreadDeath is never caught.

The class ThreadDeath is specifically a subclass of Error rather than Exception, even though it is a "normal occurrence", because many applications catch all occurrences of Exception and then discard the exception.

Class ThreadDeath

Community
  • 1
  • 1
Hülya
  • 3,353
  • 2
  • 12
  • 19
1

Thread.isAlive() helps you to check if it is alive.

Thread.getState() will help you to get the exact state it is in.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34
0

You can use monitoring tools which are shipped with jdk like jstack, jconsole to check status of running threads. Alternatively you can take thread dump and check running thread status. On Linux you can do something like this kill -3 pId where pId is your java process running id.

Mukesh Verma
  • 524
  • 5
  • 9