title: Calling a function every 2 minutes
body: <p>I'm working on a plugin for a Minecraft server which automatically breaks melons for melon farms. It loops through all the blocks in all players' current chunks, and breaks the blocks that are melons. At first, I attempted to continuously call this function with a <code>while</code> loop in the <code>onEnable</code> method, however that caused the server to time/lag out. Without the <code>while</code> loop (only calling the function once from the <code>onEnable</code>), the plugin worked fine. Every time I reloaded the server, the function would run and all melons would be broken; so I decided to make a timer that would call the function every two minutes. For some reason, the server still times out, even with the timer, which I don't understand</p>
  
  <p>Here's my code:</p>
  
  <pre><code>package me.spigot.itiurray.main;
  
  import org.bukkit.Bukkit;
  import org.bukkit.Chunk;
  import org.bukkit.Material;
  import org.bukkit.block.Block;
  import org.bukkit.entity.Player;
  import org.bukkit.plugin.java.JavaPlugin;
  
  public class Main extends JavaPlugin {
      private long goTime = 0;
  
      @Override
      public void onEnable() {
          getLogger().info("MelonDrop has been enabled.");
          startBreakWithInterval();
      }
  
      private void breakMelons() {
          for (Player player : Bukkit.getOnlinePlayers()) {
              Chunk chunk = player.getLocation().getChunk();
  
              int x = chunk.getX() &lt;&lt; 4;
              int z = chunk.getZ() &lt;&lt; 4;
  
              for (int xx = x; xx &lt; x + 16; xx++) {
                  for (int zz = z; zz &lt; z + 16; zz++) {
                      for (int yy = 0; yy &lt; 256; yy++) {
  
                          Block block = chunk.getBlock(xx, yy, zz);
  
                          if (block.getType().equals(Material.MELON_BLOCK))
                              block.breakNaturally();
  
                          goTime = System.currentTimeMillis() + 120000;
                      }
                  }
              }
          }
      }
  
      private void startBreakWithInterval() {
          boolean running = true;
  
          while (running == true) {
              if (System.currentTimeMillis() &gt;= getGoTime())
                  breakMelons();
          }
      }
  
      private long getGoTime() {
          return goTime;
      }
  }
  </code></pre>
  
  <p>Edit: Here's what it currently looks like...</p>
  
  <pre><code>    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
  
      @Override
      public void onEnable() {
          getLogger().info("MelonDrop has been enabled.");    
  
          scheduledExecutorService.scheduleWithFixedDelay(() -&gt; breakMelons(), 
  2, 2, TimeUnit.MINUTES);
      }
  </code></pre>