0

I have a simple Java class that runs in a look, doing something.

public class SomeClass{

  public int counter = 0;

  public void doSomehing(){
    while(true){
      doSomethingElse();
      counter++;
    }

  }

I would like to execute some code only when the Java program is forced to stop (e.g. by hitting a terminate button in the IDE or when stopping the program with the operating system (e.g. writing the counter to a file). Is this possible? Or is the very nature of overwriting Java with a forced exit not allowing for such a exit behavior?

RalfB
  • 563
  • 1
  • 7
  • 22
  • What I would like to do it to allow a loop to continue with a job ( a very long job running for days) to continue where it has been stopped before... Are there any other, better methods to do that? So, I index 1 Mio documents and would like to store the number of the document before i exit, so that I can continue at this point next time the application runs. Of course I could just write the last id every time I successfully index, but that means 1 Mio file writes ... not that great – RalfB Jun 30 '14 at 14:41
  • You could periodically save your work in a way that can be resumed later. Probably not every single index, but perhaps every thousand or so? That way if your process is killed, you only lose a short amount of work when you restart at your last save point? – Kevin Workman Jun 30 '14 at 14:46
  • Yes , I think this is the best solution. – RalfB Jul 01 '14 at 08:47

0 Answers0