Calling a function every 10 minutes

前端 未结 5 2173
有刺的猬
有刺的猬 2020-12-05 14:05

I\'m not an expert, just a beginner. So I kindly ask that you write some code for me.

If I have two classes, CLASS A and CLASS B, and insid

5条回答
  •  再見小時候
    2020-12-05 14:39

    import java.util.Date;
    
    import java.util.Timer;
    
    import java.util.TimerTask;
    
    public class ClassExecutingTask {
    
        long delay = 10 * 1000; // delay in milliseconds
        LoopTask task = new LoopTask();
        Timer timer = new Timer("TaskName");
    
        public void start() {
            timer.cancel();
            timer = new Timer("TaskName");
            Date executionDate = new Date(); // no params = now
            timer.scheduleAtFixedRate(task, executionDate, delay);
        }
    
        private class LoopTask extends TimerTask {
            public void run() {
                System.out.println("This message will print every 10 seconds.");
            }
        }
    
        public static void main(String[] args) {
            ClassExecutingTask executingTask = new ClassExecutingTask();
            executingTask.start();
        }
    
    
    }
    

提交回复
热议问题