This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to handle/kill a hung thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to handle/kill a hung thread" Watch "How to handle/kill a hung thread" New topic
Author

How to handle/kill a hung thread

Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
Hi,

In my code a call to a server is blocking, because of this i am getting org.omg.CORBA.COMM_FAILURE and the stack trace says this is becoz of Caused by: java.net.ConnectException: Connection timed out (errno:238).

So my thread is getting stuck/hung. So my application is not coming out.

So please anyone tells me an efficient way of handling this case i.e. to kill this hung thread so the application terminates.

Thaks in advance
Asha
Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
Ahhh CORBA, I remember that..... I'm an old corba head moreso than a java head but if your thread is catching the CORBA::COMM_FAILURE exception (as it probably should/does), then from within the handler you can exit your thread via any of the standard techniques. If you elaborate on your code/scenario I can help you more.

If you have a little pseudo code, it would help.

have a nice weekend.

Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
For info, I would also suggest posting your question along with extra details about your environment to comp.object.corba (they are heavyweight corba developers

Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
HI,

This is the code snippet i am using. From main method i am spawning 10 threads, each thread will execute this run() method. Each thread will connect to differernt server, openSession() is the method which is blocked from ORB and is not returning the control. So some of the threads are getting stuck.

Could you please tell me in which section i have to add the code to terminate this hung thread. Which method should i use. Is inetrrupt method will kill the stuck thread. How do i get an handle to the stuck thread. Is Thread.currentThread() will return me the reference of a stuck thread?

Can i use
if ((Thread.currentThread() != null) && (Thread.currentThread().isAlive())) {
Thread.currentThread().interrupt();
}

the above code to kill the thread in finally block or should i do it in catch block itself. Is this the corect solution.
If i will kill the thread in catch or finally block should my cleanup statements which are in finally block will get executed.

Please help me...


// Thread's run() method from where i will make a call to server method
public void run() {
try {
// some code for getting the corba object
obj_ref.openSession(user, password); // This is where i am getting org.omg.CORBA.COMM_FAILURE
pingT = new pingTask(sessionRef, Thread.currentThread());
timer.schedule(pingT, 10000, 3*1000);

} catch (InterruptedException e){
log.warn( "Interrupted explicitly ..", e);
} catch (Exception e){
log.error("Exception in run method", e);//
} finally {
timer.cancel();
// Some code here for cleanup
}

}

Thanks
Asha

Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
Hi Asha,

I posted a reply to this earlier but it hasn't appeared, if you see two replies from me on this then its due to a delay somewhere along the line.

From what I can see you have 2 choices;

1) Does openSession return a value? If yes, test the return value and exit from your run method, thereby exiting the thread.

2) if 1) isn't possible then have openSession throw an exception. Catch the exception in your run method below and return from run(). For info, openSession sounds like it catches the CORBA exception but does nothing with it. Maybe rethrow the exception and catch it in the method below.

For info, 2) is probably the option you need here.

public void run() {
try {
// some code for getting the corba object
obj_ref.openSession(user, password); // This is where i am getting org.omg.CORBA.COMM_FAILURE
pingT = new pingTask(sessionRef, Thread.currentThread());
timer.schedule(pingT, 10000, 3*1000);

} catch (InterruptedException e){
log.warn( "Interrupted explicitly ..", e);
} catch (CORBAException ce)
{
logg.error ("CORBA connection problem: " + ce.ToString());
return;
} catch (Exception e){
log.error("Exception in run method", e);//
} finally {
timer.cancel();
// Some code here for cleanup
}

}
Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149
Asha Santhosh wrote:Hi,
So my thread is getting stuck/hung. So my application is not coming out.


Use daemon threads for CORBA comms. Your app will terminate if only daemon threads are running.
Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
Maris Orbidans wrote:
Asha Santhosh wrote:Hi,
So my thread is getting stuck/hung. So my application is not coming out.


Use daemon threads for CORBA comms. Your app will terminate if only daemon threads are running.


I am spawning 10 threads will it not create any problem if i will make all the threads as daemon threads.
Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
Thanks for your reply!!

If i say return will the thread will also get exited or it will be hanging around. Does return statemnet will kill the hung thread.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
If the Thread can exit the run method, it must be no longer "hung" - exiting run will let the JVM clean up and eventually discard the Thread, assuming you are not doing anything tricky with a Thread pool.

Cleaning up other objects you created to be used in the run method is up to you.

Bill
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Hi "Warrent Buffoon",

Kindly check your PMs regarding an important administrative matter.
You can do the same using the My Private Messages link at the top right corner of this page.


apigee, a better way to API!
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73

obj_ref.openSession(user, password); // This is where i am getting org.omg.CORBA.COMM_FAILURE


Just wondering about the underlying implementation of the above method. What makes the thread blocking? is it waiting on I/O

What is the ojb_ref type?



Regards,
Alan Mehio
London, UK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to handle/kill a hung thread