How to close the application correctly

I have a thread in my application that constantly starts a pulse every 5 minutes for a monitoring application.

If my application crashes in the exception block, I need to exit the thread that starts it (while loop is based on interrupting the thread).

Does this mean that in every catch I need a system output or a call to interrupt this thread? This seems like a very dirty approach, but I don't know how else.

thank

+5
source share
3 answers

you can designate this thread as a daemon thread. It will be completed using the application.

+4
source

, , . , finally, , heartbeat, .

heartbeat "" . , , .

+1

, .

, while. :

while(expression)

boolean, :

while(Thread.currentThread.interrupted()){
  if(!expression)
     break;
//....
}

That would be the perfect solution. Or you can use some booleans to also notify. Or perhaps if yours whileis queue based, add POISON_PILL, which will represent that it is time to exit the loop.

Daemon threadalso the solution mentioned above, but it depends on whether you want to store some data or close / manage some resources. Then it will be a problem.

Ultimately, to ensure reliability, you need to develop an appropriate shutdown policy.

+1
source

All Articles