Does cancelling a BukkitRunnable dispose of the object?

Discussion in 'Spigot Plugin Development' started by Strahan, Sep 12, 2019 at 2:55 AM.

  1. Strahan

    Benefactor

    I was just wondering... in my runnable, when I do this.cancel(); does that dispose of the object all together or does it just stop the timer? I ask because I was thinking of a process that is like:

    Condition is met (say, player kills a specific mob)
    new (Runnable Class).runTaskTimer(this, 0, 20);
    In the runnable, it checks for a terminating condition. Condition is met, so it does this.cancel
    Another player kills mob and new runnable fires off

    Do I now have two runnable objects in memory or did the first one dispose itself upon cancel? Thanks!
     
  2. Im probably not the best one to answer this, but I'll give it a try.
    Upon looking at the CB code, the tasks are stored in a map, when you cancel a task, its removed from the map.
    My assumption would be at that point the object would be disposed of.

    EDIT:
    I very well could be wrong, Im reading over it a bit more, and it almost seems like its not removed!?!? Im confused.
     
  3. Objects in Java get deleted when there are no more references pointing to it as far as I know. If it's removed from a list, that there's no active task or thread using it and that no variables are pointing to it, it should get garbage collected.
     
  4. It probably disposes itself. As RedstoneTek said, it would get GC'd unless there is a reference pointing to it. It appears that you're instantiating that class, but you didn't set a reference for it.
     
  5. Strahan

    Benefactor

  6. SteelPhoenix

    Junior Mod

    Afaik the BukkitRunnable class stores a BukkitTask in a field which gets cancelled and set to null when using BukkitRunnable#cancel(). The BukkitRunnable instance itself gets cleaned up by the gc once it's no longer referenced anywhere.