Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:尽管处理异常,线程仍会停止_Java_Multithreading_Exception - Fatal编程技术网

Java:尽管处理异常,线程仍会停止

Java:尽管处理异常,线程仍会停止,java,multithreading,exception,Java,Multithreading,Exception,我使用Timer.schedule()定期调用TimerTask类的run()方法来轮询设备。有时会抛出一个MalformedJsonException或一个IllegalStateException,这是在catch块中处理的。线程应该在处理异常后继续轮询设备,但它会停止。 如果没有错误,将按预期定期调用run方法。 我还尝试从catch块调用runModulesPoll()方法,但没有帮助 private static void runModulesPoll(Boiler boiler) {

我使用Timer.schedule()定期调用TimerTask类的run()方法来轮询设备。有时会抛出一个MalformedJsonException或一个IllegalStateException,这是在catch块中处理的。线程应该在处理异常后继续轮询设备,但它会停止。 如果没有错误,将按预期定期调用run方法。 我还尝试从catch块调用runModulesPoll()方法,但没有帮助

private static void runModulesPoll(Boiler boiler) {
        new Timer("Modules Poll Flow").schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Module[] modules = boiler.getCore().getModules();
                    for (Module module : modules) {
                        String response = ControllersService.sendMessage(MessageBuilder.buildDataRequest(module.getAlias(), boiler.getBoilerMode()));
                        if (AppUtils.isStringInvalid(response)) {
                            module.setOnline(false);
                            ModulesResetService.reset();
                            continue;
                        }
                        module.setOnline(true);
                        module.fromShortJson(response);
                    }
                    MqttService.publishMessage(MqttMessageFactory.createDataMessage(boiler.getCore().getModulesDataAsJson()));
                } catch (Throwable e) {
                    LoggerLocal.error("Exception in Modules Poll Flow: " + e.getLocalizedMessage());
                    e.printStackTrace();
                }
            }
        }, 0, 1);
    }
根据日志,异常按预期处理,但线程不会继续轮询

    14-11-2019 18:10:47 -- Exception in Modules Poll Flow: Not a JSON Object: "hgjhgjhg"
java.lang.IllegalStateException: Not a JSON Object: "hgjhgjhg"
    at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)
    at eezo.AppUtils.getJsonObjectFromString(AppUtils.java:101)
    at eezo.services.ControllersService.handleIfErrorMessage(ControllersService.java:148)
    at eezo.services.ControllersService.sendMessage(ControllersService.java:53)
    at eezo.ApplicationRunner$1.run(ApplicationRunner.java:107)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
更新: 异常在try块内抛出,并在catch中处理,但线程停止,没有任何其他异常。 LoggerLocal根本不产生异常

我用一个更简单的例子模拟了这种情况,一切都如预期的那样工作,线程不会崩溃,并且不断地处理异常

private static void run(String[] args) {
    final int[] i = {0};
    new Timer("Modules Poll Flow").schedule(new TimerTask() {
        @Override
        public void run() {
            try {
                System.out.println("run() " + args);
                if (i[0] == 5) throw new IllegalStateException("ssss");
                i[0]++;
            } catch (Exception e) {
                System.out.println("Exception in Modules Poll Flow: " + e.getLocalizedMessage());
                e.printStackTrace();
            }
        }
    }, 0, 1);
}

这就是生成堆栈跟踪的代码吗?这很难想象,因为您的异常显然是在匿名类的
run()
方法之外传播的,而这在
catch(Throwable)中是不可能发生的
不会重新播放。可能
LoggerLocal
正在重新播放exception@CarlosHeuberger然后,
LoggerLocal
类将在堆栈中trace@ernest_k堆栈跟踪不反映异常创建的时间吗?(Throwable:“Throwable包含创建线程时线程执行堆栈的快照。”@CarlosHeuberger很好。但是
LoggerLocal.error
方法不接受异常对象
e
,因此它不可能重新调用它。