File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes How to identify if a particular Thread is hanged? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to identify if a particular Thread is hanged?" Watch "How to identify if a particular Thread is hanged?" New topic
Author

How to identify if a particular Thread is hanged?

Zang Topher
Greenhorn

Joined: Sep 24, 2012
Posts: 14
HI,

I have a requirement to to kill a request (Thread process) gracefully on User's demand. If Thread is working fine then I can process graceful exit of thread on kill request by writing some specific method for closing the connection and all.
we are also tracking thread process and keep on updating a table time to time (say every XXX second). But if a thread is hanged for hour (or more) in between of any process (say DB processing) then ofcourse the table won't get updated. In this case, how can we idetify the hanged thread and make graceful exit on user's demand(Killing request).

I have got stucked because of this issue.please help.
Hope your quick attention in this.

Many Thanks.
Zang Topher
Greenhorn

Joined: Sep 24, 2012
Posts: 14
Hi All,

Please help me in the above problem. I m not getting any idea to resolve it.

Many Thanks..
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2420
    
  50
Remember PatienceIsAVirtue

What do you mean by "thread is hanged"?
Have you looked at the thread interrupt() method?
Zang Topher
Greenhorn

Joined: Sep 24, 2012
Posts: 14
Agreed, Patience Is A Virtue.

Tony,

Thanks for your reply.
Yup I went through interrupt() and got idea about it.
But in My case its not clear that when to call interrupt().
As I explained above, request thread keeps on updating the status into taable every XXX second.
What if, thread has not updated the status in table since long (and thread has also not finished the process)? That is why I termed 'Thread is hanged' .
In this scenerio what could be the proper solution?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

You can't tell from your program whether a particular thread is hung. If you're experiencing the situation where the thread is supposed to be updating a database periodically but it stops doing that and becomes unresponsive, then there's something wrong with your system and/or the environment it's running in and you ought to look at fixing that problem.

At any rate if you've got a design requirement where the user is allowed to cancel a thread at any time, then you'd do that by calling the interrupt method. But you already knew that because you read the documentation. The other half of the work is that the cancelled thread has to recognize that it has been interrupted, and act accordingly (i.e. by shutting down). Check the API documentation and you'll find another method which is suitable for that.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Zang Topher wrote:What if, thread has not updated the status in table since long (and thread has also not finished the process)? That is why I termed 'Thread is hanged' .

Welcome to CodeRanch!

As Paul has mentioned, it's quite difficult to understand the reason just by looking at the code. However, I would like to pin-point few things:

Firstly, what you termed 'hanged' might not be a system issue. Before troubleshooting the system, firstly, make sure that there is no problem with the code.

Now, the scenario you've mentioned - is it possible? I mean, does your code contains any part in which the thread does not update DB? Or thread waits for a specific condition to occur?

Secondly, after making sure that code works properly, put a lot of logging/print statements in your code(print each and every exception in the log - even if you are re-throwing it). Observe the logs. Is everything happening as per expectation (i.e. the flow)? e.g. a thread should not wait for a condition if the condition is already occurred.

If still you don't get lucky enough to understand the problem, but get lucky enough to reproduce the scenario, then take the thread dump (typically it is `kill -3 <PID of JVM>`) - unfortunately, I'm aware of this approach only on UNIX/Linux systems - not sure about other systems. But if it is possible to get the thread dump, then observe if there are any 'blocked' threads (especially the threads created via application). By the way, thread-dump output goes to stdout stream - so, it may be your console, or log file.

In case of deadlock, or 'system hung' kind of issues, I always follow this approach - and I need to take thread dump very rarely (most of the issues are identified at logging/print stage).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Also, if you do have a thread which is stuck because of some system problem, it's quite likely that it won't respond to being interrupted either. If that happens then you're going to have to have a person intervene, with something like what Anayonkar said.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

First of all, I agree with the suggestions that you should try to figure out why the thread is hanging and fix it.

But there are some cases, say a flaky network that just aren't your fault. That specific one probably needs a socket time out.

One way to tell if a thread is hung is to have a public volatile member that the thread sets to the current system time periodically, say once each time through the loop where you think it's hanging.

Then the monitor thread looks at that value and if it is greater than some value, you can assume that the thread is unresponsive.

Joe


It's not what your program can do, it's what your users do with the program.
Zang Topher
Greenhorn

Joined: Sep 24, 2012
Posts: 14
Thanks Paul, Anayonkar and Joe for your support.

Your ideas really helped me a lot in order to trace the problem out. After analyzing all code based on the suggestion given by you all, I found a situation where thread is getting stuck (hang).
The situation is as below:
When Thread is trying to access the table to perform the operation, it is being used by other thread (not sure how long it will use). Since target table is lock so my thread is not able to make progress.

To resolve this, I thought to put a check before accessing to table and if it is lock, we will send a mail saying that table is lock.
Will it be a good approach?
Please suggest me if there is any better way to resolve this problem.

Many Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

You could do that, and it might be useful in tracking down your problem. However your problem is that something is locking a database table. (That table you mentioned, it was a database table, right?) Locking a database table is rarely a good strategy, although I suppose you might have to do it occasionally. But the usual thing to do is to use database transactions when you have multiple users accessing the database at the same time. So you should probably review your design and see whether it is locking the table as a crude method of managing simultaneous updates. If it is, you should fix that.
Zang Topher
Greenhorn

Joined: Sep 24, 2012
Posts: 14
Yes Paul, that's database table only.
Yet to finalize the way. working on it.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

The usual way to do a soft shutdown of a thread is to add a "OKtoRun" AtomicBoolean and check it each time through the main loop. You then can set the flag to "nope, dont' run" and the program exits. You're DBMS table lock is very heavy weight, and may take so long that it looks like the thread is hung when you are just going through the DBMS overhead.

Of course, Turing shows us that its impossible to tell if a program will halt.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to identify if a particular Thread is hanged?