具有以固定速率运行的可运行对象的 java 项目可以在一段时间后停止吗?我的在大约 40 小时后一直冻结

Can a java project with a runnable that runs at a fixed rate stop after a while? Mine keeps freezing after about 40 hours

自学 java 之后,我开始了一个项目,通过 api 调用一个名为 torn 的游戏从网站获取数据。由于一些帮助,我修复了一些小细节,但我遇到的主要问题仍未解决。 运行 一天半后,程序就死机了。到目前为止我找不到任何相关信息。我做了一段时间的堆转储,我注意到了一些事情。希望有人能帮忙。在第一天左右,一切都很好(3 hours and after 25 hours). Then, a few hours later, the program is still running but there is no instance of the method that runs it all (screenshot after 30 hours). A few hours after that the program is still running (as in it has not terminated or exited) but there is no activity and no instances of the methods at all (40 hours into run 之后的堆转储屏幕截图)。 (某些图像可能需要左右滚动才能查看所有信息)。 我还注意到程序卡死后,runnable的线程从"timed-waiting"变成了"waiting",我也不明白。

我还包括了 code for my project(减去连接到站点时使用的实际密钥)以及图像,以防它 helps.The 主要在 OtherFactionsStats.java 中。

感谢所有的帮助和建议 - 特别是我在 java 中的初学者身份 - 在此先感谢您。

如果 RunUpdater 构造函数抛出 RuntimeException,就会发生这种情况。

根据 JavaDoc:

The sequence of task executions continues indefinitely until one of the following exceptional completions occur:

  • The task is explicitly cancelled via the returned future.
  • The executor terminates, also resulting in task cancellation.
  • An execution of the task throws an exception. In this case calling get on the returned future will throw ExecutionException.

Subsequent executions are suppressed. Subsequent calls to isDone() on the returned future will return true.

我建议替换(在 OtherFactionStats.main() 中):

service.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.SECONDS);

ScheduledFuture<?> scheduledFuture = service.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.SECONDS);
try {
    scheduledFuture.get();
} catch (InterruptedException e) {
    System.out.println(e);
    e.printStackTrace();
} catch (ExecutionException e) {
    System.out.println(e);
    e.printStackTrace();
}
service.shutdown();

这将打印出 new RunUpdater() 期间发生的任何异常,然后优雅地关闭您的应用程序。