Calling a function every 10 minutes

前端 未结 5 2161
有刺的猬
有刺的猬 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:15
    public class datetime {
    
        public String CurrentDate() {
    
            java.util.Date dt = new java.util.Date();
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
            String currentTime = sdf.format(dt);
            return currentTime;
    
        }
    
        public static void main(String[] args) {
            class SayHello extends TimerTask {
    
                datetime thisObj = new datetime();
    
                public void run() {
                    String todaysdate = thisObj.CurrentDate();
                    System.out.println(todaysdate);
                }
            }
            Timer timer = new Timer();
            timer.schedule(new SayHello(), 0, 5000); 
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:18

    Try this. It will repeat the run() function every set minutes. To change the set minutes, change the MINUTES variable

    int MINUTES = 10; // The delay in minutes
    Timer timer = new Timer();
     timer.schedule(new TimerTask() {
        @Override
        public void run() { // Function runs every MINUTES minutes.
            // Run the code you want here
            CLASSB.funcb(); // If the function you wanted was static
        }
     }, 0, 1000 * 60 * MINUTES);
        // 1000 milliseconds in a second * 60 per minute * the MINUTES variable. 
    

    Don't forget to do the imports!

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

    For more info, go here:

    http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

    0 讨论(0)
  • 2020-12-05 14:36

    Have a look at the ScheduledExecutorService:

    Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

     import static java.util.concurrent.TimeUnit.*;
     class BeeperControl {
        private final ScheduledExecutorService scheduler =
           Executors.newScheduledThreadPool(1);
    
        public void beepForAnHour() {
            final Runnable beeper = new Runnable() {
                    public void run() { System.out.println("beep"); }
                };
            final ScheduledFuture<?> beeperHandle =
                scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
            scheduler.schedule(new Runnable() {
                    public void run() { beeperHandle.cancel(true); }
                }, 60 * 60, SECONDS);
        }
     }
    
    0 讨论(0)
  • 2020-12-05 14:37

    Solution with Java 8

    ClassB b = new ClassB();    
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    Runnable task = () -> {
        b.funb();
    };
    executor.scheduleWithFixedDelay(task, 0, 10, TimeUnit.MINUTES);
    
    0 讨论(0)
  • 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();
        }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题