• 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

How to identify if a particular Thread is hanged?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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

Many Thanks..
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember PatienceIsAVirtue

What do you mean by "thread is hanged"?
Have you looked at the thread interrupt() method?
 
Zang Topher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Zang Topher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Paul, that's database table only.
Yet to finalize the way. working on it.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic