Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle/kill a hung thread

 
Ashuthosh san
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic