我应该如何终止Java中的循环子线程?
当我要终止循环线程时,我需要做一些清理工作。例如保存缓冲区以便我稍后可以继续。
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
那么实现这个的最佳方法是什么?
- 使用标志:但问题是如果resumeCalculate()需要(编辑:很长一段时间)
永远结束怎么办? - 将异常放入计算器,如何?
- 我可以使用事件监听器之类的吗?我以为它们是用于 GUI
- 就停止吧? 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?
- Use a flag: But the problem is what if resumeCalculating() takes (EDIT: a long time)
foreverto end? - Put an exception into calculator, how?
- Can I use event listeners or something? I thought they were used for GUIs
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
停止线程的正确方法是 打断它。
如果线程中运行的任务正在执行 IO 或正在使用
sleep
,那么它将收到信号(此时InterruptedException
);否则任务应该定期轮询以查看其 中断。让我们调整原始海报的伪代码:
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:
对 1 的回答:您也可以在
resumeCalculated()
中处理中止标志。Answer to 1.: You can process the abort flag in
resumeCalculating()
too.最好使用一个标志,等待一段时间让线程结束,如果还没有结束(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.
设计您的程序,以便恢复计算不会永远持续下去。另外,同步对您的标志的访问。
Design your program so that resumeCalculating does NOT take forever to continue. Also, synchronize access to your flag.