我应该如何终止Java中的循环子线程?

发布于 2024-08-22 09:29:04 字数 1073 浏览 8 评论 0原文

当我要终止循环线程时,我需要做一些清理工作。例如保存缓冲区以便我稍后可以继续。

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

那么实现这个的最佳方法是什么?

  1. 使用标志:但问题是如果resumeCalculate()需要(编辑:很长一段时间)永远结束怎么办?
  2. 将异常放入计算器,如何?
  3. 我可以使用事件监听器之类的吗?我以为它们是用于 GUI
  4. 就停止吧? Thread 类包含某种解构函数,当线程终止时将调用该解构函数,我可以在那里进行处理?

所有者编辑:

我知道我可以使用标志。但请考虑一下:

    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
       }
    }

在每行之间放置 if(flag) saveResultsAndExit(); 实用还是美观? 我只想扔掉处理了一半的结果,保存之前的结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

じее 2024-08-29 09:29:04

停止线程的正确方法是 打断它。

如果线程中运行的任务正在执行 IO 或正在使用 sleep,那么它将收到信号(此时 InterruptedException);否则任务应该定期轮询以查看其 中断

让我们调整原始海报的伪代码:

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

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
逆光飞翔i 2024-08-29 09:29:04

对 1 的回答:您也可以在 resumeCalculated() 中处理中止标志。

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

泪是无色的血 2024-08-29 09:29:04

最好使用一个标志,等待一段时间让线程结束,如果还没有结束(resumeCalculated 尚未返回),则手动终止线程。最好不要在恢复计算中涉及太多基于线程的逻辑,这实际上取决于它的实现方式以及在操作中途中止的容易程度。

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.

陪我终i 2024-08-29 09:29:04

设计您的程序,以便恢复计算不会永远持续下去。另外,同步对您的标志的访问。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文