954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to make a thread never be terminated?

Hello everyone,


I have a very important thread in my application and I do not want it to be terminated accidently, or in other words, if it is terminated for some reason (for example, be terminated by some unhandled Runtime exceptions), I want it to restart automatically.

I am wondering whether Java has build-in approach to implement this goal. If Java does not have build-in approach to implement this, I am wondering where can I find similar sample source codes or tutorials?


regards,
George

George2
Junior Poster
189 posts since Nov 2004
Reputation Points: 11
Solved Threads: 0
 

Well, if the JVM dies any thread in it dies as well so you can't have a thread that never dies at all.

Other than that, you can track whether your thread is alive of course.
Thread has a method for that called isAlive() which returns true if the thread is not dead (but has been started).
You can of course NOT restart a dead thread, so if it dies you'll have to create a new thread for it and start that.

I've found good threading tutorials very hard to find. I'm also still looking for a good threading book, the one I did get (Taming Java Threads) is probably the worst book on programming I've ever had the displeasure of reading and is the first programming book I ever tossed into the trash that wasn't completely outdated.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Thanks jwenting,

Well, if the JVM dies any thread in it dies as well so you can't have a thread that never dies at all.

Other than that, you can track whether your thread is alive of course. Thread has a method for that called isAlive() which returns true if the thread is not dead (but has been started). You can of course NOT restart a dead thread, so if it dies you'll have to create a new thread for it and start that.

I've found good threading tutorials very hard to find. I'm also still looking for a good threading book, the one I did get (Taming Java Threads) is probably the worst book on programming I've ever had the displeasure of reading and is the first programming book I ever tossed into the trash that wasn't completely outdated.

Both your reply and your advice are very helpful.


regards,
George

George2
Junior Poster
189 posts since Nov 2004
Reputation Points: 11
Solved Threads: 0
 

Thanks for all the people who helped me on this thread.

Thanks jwenting,

Both your reply and your advice are very helpful.

regards, George


regards,
George

George2
Junior Poster
189 posts since Nov 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You