我在我们的生产环境中遇到了特殊情况,其中一段特定的代码进入了无限循环。原因主要是数据特定的,无法找出真正的原因。同时,我希望做的是产生一个单独的子线程来执行那段代码,如果它执行超过 30 秒,想要阻止该子线程执行
Thread t = new Thread() {
public void run() {
// This is where i will the method that runs in a infinite loop
callMethodThatRunsInInfiniteLoop();
};
};
t.start();
try {
t.join(2000); // wait 2s
} catch (InterruptedException e) {
e.printStackTrace();
}
// if not completed how to break the child thread ???