可运行任务解析传入的 xml 文件并从不同的类调用。有时解析可能会失败并抛出异常。即使发生异常,任务也应该运行。我尝试使用未捕获的异常处理程序在新线程中重新启动相同的任务。但想要更多关于这方面的想法。
类调用线程:(调用线程)
在新线程中重新启动相同的任务效果很好,但可能应该在不导致线程退出的情况下处理异常
Thread fileProcessThread = new Thread(FileProcessor);
fileProcessorThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread arg0, Throwable arg1)
{
FileProcessor newObject = new FileProcessorTask();
Thread t = new Thread(newObject);
t.start();
}
});
fileProcessor.start();
任务类别:
public void run() {
try {
xmlparser.parse(incomingXmlFile);
}
catch (Exception e) {
Thread.currentThread.getUncaughtExceptionalHandler().uncaughtException(Thread.currentThread(), e);
// this invokes uncaughtException to restart thread ?
}
}
我有一个监视服务(文件目录扫描)正在运行,因此我始终需要该任务,即使线程终止也是如此。
最佳答案
当发生异常并且调用到达uncaughtExceptionHandler时,线程的状态为Invalid,无法再次启动。所以你需要创建一个新线程并重新开始。
来自Thread.start()的代码
// A zero status value corresponds to state "NEW".
if (threadStatus != 0)
throw new IllegalThreadStateException();
但是这很容易导致无限循环。 (异常 -> catch -> 重试 -> 异常 -> catch ...) 我建议使用一个计数器,在某个点后停止重试。
Public class TestClass{
static AtomicInteger counter = new AtomicInteger();
static class MyExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught");
if (counter.get() == 3) {
System.out.println("Reached Max. retries, exiting");
} else {
counter.incrementAndGet();
new Thread(new MyTask()).start();
}
}
}
static class MyTask implements Runnable {
@Override
public void run() {
try {
Thread.currentThread().setUncaughtExceptionHandler(new MyExceptionHandler());
System.out.println("slept");
Thread.sleep(500);
double d = 0 / 0;
} catch (InterruptedException e) {}
}
}
public static void main(String args[]) throws Exception {
Thread thread = new Thread(new MyTask());
thread.start();
}
}
我使用了static AtomicInteger
,但在您的实现中可能有一个公共(public)对象,可以从一个线程传递到另一个线程,并让该对象有一个计数器。
关于java - 使用未捕获的异常处理程序在异常上启动新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555730/