我有一个 Java 方法,它对输入集执行两个计算:估计的和准确的答案。估算值总是可以在可靠的时间内廉价地计算出来。有时可以在可接受的时间内计算出准确的答案,有时则不能(先验未知......必须尝试看看)。
我要设置的是一些框架,如果准确的答案花费太长时间(固定超时),则使用预先计算的估计值。我想我会为此使用一个线程。主要的复杂性是计算准确答案的代码依赖于外部库,因此我不能“注入”中断支持。
这个问题的独立测试用例在这里,展示了我的问题:
package test;
import java.util.Random;
public class InterruptableProcess {
public static final int TIMEOUT = 1000;
public static void main(String[] args){
for(int i=0; i<10; i++){
getAnswer();
}
}
public static double getAnswer(){
long b4 = System.currentTimeMillis();
// have an estimate pre-computed
double estimate = Math.random();
//try to get accurate answer
//can take a long time
//if longer than TIMEOUT, use estimate instead
AccurateAnswerThread t = new AccurateAnswerThread();
t.start();
try{
t.join(TIMEOUT);
} catch(InterruptedException ie){
;
}
if(!t.isFinished()){
System.err.println("Returning estimate: "+estimate+" in "+(System.currentTimeMillis()-b4)+" ms");
return estimate;
} else{
System.err.println("Returning accurate answer: "+t.getAccurateAnswer()+" in "+(System.currentTimeMillis()-b4)+" ms");
return t.getAccurateAnswer();
}
}
public static class AccurateAnswerThread extends Thread{
private boolean finished = false;
private double answer = -1;
public void run(){
//call to external, non-modifiable code
answer = accurateAnswer();
finished = true;
}
public boolean isFinished(){
return finished;
}
public double getAccurateAnswer(){
return answer;
}
// not modifiable, emulate an expensive call
// in practice, from an external library
private double accurateAnswer(){
Random r = new Random();
long b4 = System.currentTimeMillis();
long wait = r.nextInt(TIMEOUT*2);
//don't want to use .wait() since
//external code doesn't support interruption
while(b4+wait>System.currentTimeMillis()){
;
}
return Math.random();
}
}
}
这工作正常输出...
Returning estimate: 0.21007465651836377 in 1002 ms
Returning estimate: 0.5303547292361411 in 1001 ms
Returning accurate answer: 0.008838428149438915 in 355 ms
Returning estimate: 0.7981717302567681 in 1001 ms
Returning estimate: 0.9207406241557682 in 1000 ms
Returning accurate answer: 0.0893839926072787 in 175 ms
Returning estimate: 0.7310211480220586 in 1000 ms
Returning accurate answer: 0.7296754467596422 in 530 ms
Returning estimate: 0.5880164300851529 in 1000 ms
Returning estimate: 0.38605296260291233 in 1000 ms
但是,我有一个非常大的输入集(大约数十亿个项目)来运行我的分析,我不确定如何清理未完成的线程(我不希望它们在背景)。
我知道有充分的理由不推荐使用各种销毁线程的方法。我也知道停止线程的典型方法是使用中断。但是,在这种情况下,我看不到我可以使用中断,因为该run()
方法将单个调用传递给外部库。
在这种情况下如何杀死/清理线程?