可以固定速率运行的可运行对象的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

查看:94
本文介绍了可以固定速率运行的可运行对象的Java项目过一会儿可以停止吗?约40小时后,矿井保持冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己学习了Java之后,我开始了一个项目,该项目通过api调用一个叫做torn的游戏从网站获取数据.在某些帮助下,我修复了一些小细节,但我遇到的主要问题仍未解决.经过一天半的运行,该程序将冻结.到目前为止,我什么都找不到.我花了一段时间堆堆,我注意到了一些事情.希望有人可以帮忙.在第一天左右,一切都很好(在

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.

我还为我的项目添加了代码(减去用于连接到网站)以及图片(如果有帮助的话).主要在OtherFactionsStats.java中.

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 .

我非常感谢您提供的所有帮助和建议,尤其是对于Java的初学者身份,并在此先感谢您.

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

推荐答案

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

This can happen if the RunUpdater constructor throws a RuntimeException.

根据

任务执行的顺序将无限期继续,直到发生以下异常完成之一:

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

  • 通过返回的Future明确取消了任务.
  • 执行器终止,也导致任务取消.
  • 任务的执行会引发异常.在这种情况下,在返回的Future上调用get将会抛出ExecutionException.
  • 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.

后续执行被抑制.在返回的future上对isDone()的后续调用将返回true.

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()期间发生的所有异常,然后正常关闭您的应用程序.

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

这篇关于可以固定速率运行的可运行对象的Java项目过一会儿可以停止吗?约40小时后,矿井保持冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆