0

可运行任务解析传入的 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 ?
            }
      }

我有一个监视服务(文件目录扫描)正在运行,所以我一直需要这个任务,即使线程终止。

4

1 回答 1

1

当发生异常,调用到达uncaughtExceptionHandler时,线程的状态为Invalid重新启动。所以你需要创建一个新线程并重新开始。

来自Thread.start()的代码

// A zero status value corresponds to state "NEW".
if (threadStatus != 0)
   throw new IllegalThreadStateException();

但是,这很容易导致无限循环。(异常 -> 捕获 -> 重试 -> 异常 -> 捕获 ...)我建议有一个计数器在某个点后停止重试。

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,但在您的实现中可能有一个公共对象,可以从一个线程传递到另一个线程并让该对象有一个计数器。

于 2017-01-09T20:53:29.003 回答