Solved Scheduled Task running slow?

Discussion in 'Plugin Development' started by KiF, Feb 4, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    KiF

    Hello, I'm trying to run a timer for my plugin. It's currently setup like this:
    Code:
    public void respawnTimer() {
            if (isRespawnTimerOn) {
                getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                    public void run() {
                        if (respawnTime > 0) {
                            respawnTime--;
                            if (respawnTime == 4800 || respawnTime == 3600 || respawnTime == 2400 || respawnTime == 1200) {
                                Bukkit.broadcastMessage("ยง9HUMANS WILL GET REINFORCMENTS IN " + (respawnTime / 1200) + " MINUTES!");
                            }
                        }
                        if (respawnTime == 0) {
                            for (Player players : Bukkit.getOnlinePlayers()) {
                                if (board.getTeam("Humans").hasPlayer(players) == true) {
                                    if (players.getGameMode() == GameMode.SPECTATOR) {
                                        humanReinforce(players);
                                    }
                                }
                            }
                            isRespawnTimerOn = false;
                        }
                    }
                }, 0, 20);
            }
        }
    I wasn't sure at first what was wrong with it. It seemed like it didn't run, but there weren't any errors in console. After being afk for a while on my server, I realized the timer went down by a "minute," when ~20 had passed. What is wrong with my timer?
     
  2. Offline

    Raydond123

    @KiF
    Isn't the method, scheduleAsyncRepeatingTask, deprecated?
    Try using schedulerSyncRepeatingTask instead.
     
  3. Offline

    KiF

    Unforetunately, that doesn't make a difference, I was just testing things.
     
  4. Offline

    sirrus86

    Why not have the task run every minute instead of every second? So far it doesn't look like anything is supposed to take place between each minute.

    EDIT: Also you task runs once per second, but your code suggests you're waiting 1200 seconds before doing anything.
     
    KiF likes this.
  5. Offline

    mythbusterma

    @KiF

    Also, you're completely violating the thread safety of the server.

    Don't run the task asynchronously unless you can clearly explain what that actually entails. You're performing tons of dangerous operations every time that code is run.

    Just make it synchronous, it shouldn't be that expensive.
     
  6. Offline

    KiF

    Thanks @sirrus86 for the suggestion. After changing it to run every 1200 ticks, everything works fine.
     
Thread Status:
Not open for further replies.

Share This Page