This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to restart a thread in case of error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to restart a thread in case of error?" Watch "How to restart a thread in case of error?" New topic
Author

How to restart a thread in case of error?

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Hi,

In our project there is a deamon thread, which keeps running all the time.

But sometimes, (Not due to bad programming and not due to application errors, but due to some unknown errors, the thread terminates somehow).
Then the client has to restart the server.

He wants us to provide an option, where he can restart the thread himself?

How this can be done? Please suggest some suitable options that we can think of...

Creating another thread for monitoring the thread in question will be one option, but can we have something better. Because creating another thread just to monitor one, looks like creating another dependency just to remove the other.

Guys, waiting for your response.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Yogesh Gandhi wrote:
But sometimes, (Not due to bad programming and not due to application errors, but due to some unknown errors, the thread terminates somehow).
Then the client has to restart the server.


I wouldn't give up on this so quickly -- there is no way that a thread can terminate for no reason.

IMO, it may be a good idea to wrap the run() method, to see how it is terminating. Maybe write a new run() method, that (1) calls the old run() method, and reports if it is exiting from it, and (2) catch everything that can be thrown (ie. Throwable) and report on that too..... You need to figure out what "terminates somehow" means, before you can conclude, "not due to bad programming and not due to application errors". Threads doesn't just disappear.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Thanks Henry for the response.

But that's what I heard from my boss. That its not due to bad programming or application errors.

And Yeah, you are right in saying we must investigate the original thread, why it terminates...

At this point in time, I have not been given the access to that part of code..

I was thinking, that there might have been some exception that might have caused the thread to terminate...
Usually Deamon thread should of the following form...



I think, Their code might not have handled the exceptions...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Yogesh Gandhi wrote:
I think, Their code might not have handled the exceptions...


Or it is doing something wrong, which is generating an exception or error -- which leads me back to how can you can conclude "not due to bad programming and not due to application errors"?

BTW, I suggest you catch Throwable instead. I had a case where someone else claimed the same thing -- and showed me code with a catch of all exceptions to prove it. I changed it to Throwable, and the catch now printed out that it couldn't find a shared libraries. A catch of Exception doesn't catch everything.

Henry
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Also, are you sure the Thread is terminating and not just 'stopped working?' They are two different things. A thread can stop working if it gets caught in an infinite loop, is blocked by some external communication, or run in to some threading issue that causes deadlock. When a thread stops like this you usually don't see an exception and just restarting the thread may not help (the same thing might just happen again right away, or you could end up with lots of background threads with different states competing for CPU cycles). To defeat this sort of thing you have to break out of the loop/deadlock/blocking before you restart the task so as to be sure things are cleaned up.


Steve
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Hey Steve,

Thanks for the response.

Actually Here is the actual scenario

We have a web-application, the EAR is clustered on 6 of the server and on the 7th server (which is not a part of cluster) runs this thread I am talking about.

So, sometimes what happens is, due to some reason (which is unknown to us at this point of time) the threads stops running. Hence the functionality which must happen due to execution of thread stops working.

The client complains and when our team investigates, they find thread is stopped. They have to restart the Application on the 7th server so as the thread again starts its execution (Please note that thread is a deamon thread, that is running always).

Now the client wants, that he should have the option of checking the status of the thread and if at all possible, he should be given an option to restart the thread from the GUI itself (incase it is already dead).

We are not sure of the exact situation (as of now)......but all what we heard from them is that they need to restart the server instance when ever they find that thread is not working.

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Now I got the access to their code.


They were using try-catch-Exception

and not

try-catch-Throwable

I will be using try-catch-Throwable and lets see if I can get hold of the reason that causes the thread to terminate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to restart a thread in case of error?
 
Similar Threads
handling errors
DeadThreads
Restarting the server
Please help me Thread Question!! :(
extend or implement?