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

huangapple go评论140阅读模式
英文:

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小时后的堆转储截图25小时后的堆转储截图)。然后,几个小时后,程序仍在运行,但没有运行整个程序的方法的实例(30小时后的截图)。再过几个小时,程序仍在运行(未终止或退出),但没有活动,也没有任何方法的实例(运行后的第40个小时)。
(某些图像可能需要左右滚动以查看所有信息)。
我还注意到在程序冻结后,可运行线程的状态从“定时等待”变为“等待”,这也让我不太理解。

我还在我的项目代码中包含了图像(连接站点的实际密钥除外),以防有帮助。主要代码在OtherFactionsStats.java中。

我非常感谢所有的帮助和建议,尤其是对我作为Java初学者的支持,提前谢谢您。

英文:

After learning java on my own, i began a project for getting data from a website through api calls for a game called torn. There were a few little details i fixed thanks to some help, but the main issue i had is still not solved. after a day and a half of running, the program just freezes. i couldn't find anything about it so far. I took heap dumps for a while and i noticed some things. hope someone can help. During the first day or so, all is well (screenshot of heap dump after 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).
(Some of the images may require scrolling right and left to see all the info).
I also noticed that after the program freezes, the thread for the runnable changes from "timed-waiting" to "waiting", which i also don't understand.

I have also included the code for my project (minus the actual key used in connecting to the site) along with the images in case it helps.The main is in OtherFactionsStats.java .

I appreciate all the help and advice -especially with my beginner status in java-, and thank you in advance.

答案1

得分: 2

这可能会发生在 RunUpdater 构造函数抛出 RuntimeException 的情况下。

根据 JavaDoc

> 任务执行的序列将无限期地继续,直到发生以下异常完成之一:
>
> * 通过返回的 future 显式取消任务。
> * 执行器终止,也会导致任务被取消。
> * 任务的执行抛出异常。在这种情况下,调用返回的 future 上的 get 将抛出 ExecutionException。
>
> 后续执行被抑制。对返回的 future 调用 isDone() 的后续调用将返回 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() 过程中打印出任何异常,并优雅地关闭您的应用程序。

英文:

This can happen if the RunUpdater constructor throws a RuntimeException.

According to the 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.

I would propose to replace (in OtherFactionStats.main()):

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

with

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();

This will print out any exception that occurs during new RunUpdater() and then gracefully shut down your application.

huangapple
  • 本文由 发表于 2020年4月6日 13:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61053452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定