Java Program To Run Something Every X Minutes

Chapter: Miscellaneous Last Updated: 22-04-2023 04:09:44 UTC


            /* ............... START ............... */
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    public static void main(String[] args) {
        int delay = 0; // delay for x minutes
        int period = 5 * 60 * 1000; // repeat every x minutes

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // put your task here
                System.out.println("Task performed on " + new java.util.Date());
        }, delay, period);

                /* ............... END ............... */


  • In this example, we first define the delay and period between executions of the task. The delay is set to 0, which means the task will start executing immediately. The period is set to 5 minutes, but you can change this to any number of minutes you like by modifying the calculation in the period variable.
  • Next, we create a new Timer object and schedule a TimerTask to run at a fixed rate using the scheduleAtFixedRate() method. The TimerTask is defined as an anonymous inner class that overrides the run() method. In this example, the run() method simply prints a message to the console indicating that the task has been performed, but you can replace this with any code you like.
  • The scheduleAtFixedRate() method takes three arguments: the TimerTask to run, the delay before the first execution, and the period between executions. In this case, the delay is set to 0 and the period is set to the value of period that we defined earlier.
  • When you run this program, the task defined in the run() method will be executed every x minutes as specified by the period variable. You can replace the print statement with any code that you want to execute repeatedly every x.


Java Program To Run Something Every X Minutes #Java schedule task until condition #Run thread every 5 seconds? (Java)

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018