简体   繁体   中英

TimerTask not executing

I am programming a discord bot and want to run an unban at a certain time. However, the TimerTask is not called even though the date is in the future. If the date is in the past, the timertask calls instant. I've tried to sout some data to look if the date is wrong but its right.

I want to call it once.

        System.out.println("started");
        System.out.println(date);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("called");
                user.openPrivateChannel().complete().sendMessage(
                        "**-Unban-**" +
                                "\n" +
                                "Du wurdest entbannt!"
                ).queue();
                guild.removeRoleFromMember(user.getId(), guild.getRoleById("690579286582624276")).queue();
                guild.addRoleToMember(user.getId(), guild.getRoleById("688733671104053327")).queue();
            }
        }, date);

Output from Console:

started
Fri Mar 20 17:21:55 UTC 2020

Hope you'll find out!

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    Date date = new Date();// Initialize it as per your requirement
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            System.out.println("called");
            //...
            timer.cancel();
        }
    };

    public void start() {
        timer.schedule(task, date);
    }

    public static void main(String[] args) {
        Main timer = new Main();
        timer.start();
    }
}

to use the timer should do the next

scheduleAtFixedRate

Example

reloj.scheduleAtFixedRate(new Reloj(), 0, 1000);

You can do the next

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication11;

import java.util.TimerTask;

/**
 *
 * @author Sem-6-INGENIERIAINDU
 */
public class Reloj extends TimerTask{

    @Override
    public void run() {
      // Your code
    }

}

The class implements the timer /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication11;

import java.util.Timer;

/**
 *
 * @author Sem-6-INGENIERIAINDU
 */
public class JavaApplication11 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Timer reloj=new Timer();
      reloj.scheduleAtFixedRate(new Reloj(), 0, 1000);

    }

}

The result is

run: Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo Corriendo BUILD STOPPED (total time: 1 minute 10 seconds)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM