views:

148

answers:

4

Hi, I'm new to Java

I need to do some clean up work when I am going to terminate a looped thread. Such as saving a buffer so I can continue later.

PseudoCode: 
private class CalculatePI(Byte[] resume) implements Runnable{
    public void Run(){
       while(true){
          resume=resumeCalculating(resume);
       }
    }
}
Thread calculator= new Thread(new CalculatePI(Byte[] resume)); 
Thread.sleep(200); 
//And now I want to kill calculator

So what's the best way implement this?

  1. Use a flag: But the problem is what if resumeCalculating() takes (EDIT: a long time) forever to end?
  2. Put an exception into calculator, how?
  3. Can I use event listeners or something? I thought they were used for GUIs
  4. Just stop it? And Class Thread includes some kind of deconstructor that will be called when the thread is terminated and I could do the processing there?

EDIT by Owner:

I know I can use a flag. But consider this:

    public void Run(){
       while(true){
          resume=calculate_stepone(resume); //takes one minute
          resume=calculate_steptwo(resume); //takes two minutes
          resume=calculate_stepthree(resume); //takes three minutes
          resume=calculate_steplast(resume); //takes four minutes
       }
    }

Is putting a if(flag) saveResultsAndExit(); between every line practical or pretty? I just want to throw away the half-processed results, and save the previous results.

A: 

Answer to 1.: You can process the abort flag in resumeCalculating() too.

Otto Allmendinger
Yes I can process the abort flag in resumeCalculating(), but that isn't as pretty I would like it to be. resumeCalculating should care only about calculating PI, not handling how it should die. That code would be best in the calculator thread. And what if I had many steps?
TiansHUo
A: 

It is probably best to use a flag, wait for a while for the thread to end, and if it hasn't (resumeCalculating hasn't returned) kill the thread manually. It is probably best not to involve too much thread based logic in resumeCalculating, it really depends on how it is implemented as to how easy it is to abort halfway through an operation.

Guy
A: 

Design your program so that resumeCalculating does NOT take forever to continue. Also, synchronize access to your flag.

Lucas Lindström
+5  A: 

The proper way to stop a thread is to interrupt it.

If the task running in the thread is performing IO or is using sleep then it will receive the signal (InterruptedException at that point); else the task should regularly poll to see if its interrupted.

Lets adapt the original poster's psuedocode:

private class CalculatePI(Byte[] resume) implements Runnable{
    public void Run(){
       while(!Thread.interrupted()) { //###
          resume=resumeCalculating(resume);
       }
    }
}
Thread calculator= new Thread(new CalculatePI(Byte[] resume)); 
calculator.run(); //###
//...
//And now I want to kill calculator
calculator.interrupt(); //### sends the signal
//...
calculator.join(); //### actually waits for it to finish
Will
yes I think that's the answer, thank you very much
TiansHUo
just don't forget that `interrupted()` also clears the *interrupted status* so subsequent calls will return `false`.
Carlos Heuberger