You can use ScheduledExecutorService to run methods or tasks periodically in java.
Check the example below.

 

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BeepControlExecutor {
    public static void main(String[] args) {
        ScheduledExecutorService execService
                =  Executors.newScheduledThreadPool(5);
        execService.scheduleAtFixedRate(()->{
            //The repetitive task, say to update Database
            System.out.println("hi there at: "+ new java.util.Date());
            method1();
        }, 0, 1000L, TimeUnit.MILLISECONDS);
    }

    public static void method1(){
        System.out.println("-----------------------------");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

WP Facebook Auto Publish Powered By : XYZScripts.com