gpt4 book ai didi

Java线程停止没有异常

转载 作者:搜寻专家 更新时间:2023-10-30 21:17:45 25 4
gpt4 key购买 nike

当我为我的程序使用 4 个线程时通常没有问题,但今天我将它增加到 8 个并且我注意到 1-3 个线程停止工作而没有抛出任何异常。无论如何要找出他们为什么停下来?有没有办法让线程重新启动?

我的线程结构是这样的

public void run()
{
Main.logger.info(threadName + ": New Thread started (inside run)");
while (true)
{
try
{
//all my code
//all my code
//all my code
}
catch(Exception e)
{
Main.logger.error("Exception: " + e);
try
{
Thread.sleep(10000);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
}
finally
{
try
{
webClient.closeAllWindows();
Thread.sleep(3000);
Main.logger.info(threadName + ": Closed browser!");
}
catch (Exception e)
{
Main.logger.error("Exception: " + e);
}
}
}// end while
}

问候!

最佳答案

请注意 Error 不是 Exception ;这是一个Throwable .
因此,如果您捕获异常错误 仍会通过:

private void m() {    
try {
m(); // recursively calling m() will throw a StackOverflowError
} catch (Exception e) {
// this block won't get executed,
// because StackOverflowError is not an Exception!
}
}

要捕获“一切”,请将您的代码更改为:

try {
...
} catch (Throwable e) {
// this block will execute when anything "bad" happens
}


请注意,如果发生错误,您可能无能为力。错误的 javadoc 摘录:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

关于Java线程停止没有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com