tags:

views:

64

answers:

3

Is there a way to know when a specific process is "stuck" in Java?

I'm running an external application from my java program. Sometimes, this app hangs. I would like to know when this app stops working so I can kill it from my code. I'm thinking of some type of monitoring from a different thread in my code.

Any toughts?

A: 

My first question would be to ask what you mean by "stuck". Is it in an infinite loop? Is it deadlock?

By the nature of the question I'm guessing you don't know, but if all you want to do is kill it when it hangs, you can have it write some output to a file periodically. Another process can monitor this file, and if some number of periods go by without seeing output, you can kill it then.

danben
Sounds like a good solution, However, I can't modify the running app because I don't have access to its code.
tou
Given that you can't modify the application, and without knowing how the application interacts with the rest of the world, or how you know that it has hung, I don't believe this question is answerable.
danben
Thanks for your answers danben. I was thinking there might be a way of to know the status of the process in the OS from Java
tou
You could monitor the CPU activity of the app, and if the CPU is close to zero for a prolonged period, kill it (assuming that when it hangs, CPU drops)
ewernli
@Carlos Blanco: on Un*x, you could at least detect some status programmatically, like the "zombie" one...
Webinator
Can the downmodder please explain?
danben
A: 

If the other app is also java, you could use the Java Service Wrapper for monitoring and restarting it in the event that it is hung.

http://wrapper.tanukisoftware.org/doc/english/download.jsp

btreat
A: 

Under java 6 you can get a thread dump for All threads. If you snapshot these every minute you Can see what goes ón.

Thorbjørn Ravn Andersen