Search code examples
javarunnablefreezeheap-dumpthread-state

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


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.


Solution

  • 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.