I have following code in java-
@Override
public void run() {
logger.info("Thread Started:" + this.getName());
try {
runJob();
} catch( Throwable e) {
logger.error("Exception in running thread: " + this.getName() + ", restarting job", e);
run();
}
}
public void runJob() {
while(true) {
try {
// Work here
} catch(Exception e) {
// log the exception
}
}
}
Is this code actually be going to keep the thread alive in every case and is this only the way to recover thread?
This is the alternative I thought of after reading all the answers. Let me know if this is a good way for keeping the thread alive forever even if Error occurred:
@Override
public void run() {
logger.info("Thread Started:" + this.getName());
while(true) {
try {
runJob();
} catch( Throwable e) {
logger.error("Exception in running thread: " + this.getName() + ", restarting job", e);
}
}
}
public void runJob() {
try {
// Work here
} catch(Exception e) {
// log the exception
}
}