我在生产环境中遇到了一个特殊的情况,其中一段代码陷入了无限循环。原因主要是数据相关的问题,无法找出真正的原因。同时,我希望能够生成一个单独的子线程来执行该代码段,并且如果它执行时间超过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 ???
Thread.stop
,但你真的应该尝试利用中断。 - obataku