• 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 restart a thread in case of error?

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic