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

∥☆過路亽.° 提交于 2020-05-15 08:11:47

问题


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:


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.



来源:https://stackoverflow.com/questions/61053452/can-a-java-project-with-a-runnable-that-runs-at-a-fixed-rate-stop-after-a-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!