Java Program To Run Something Every X Minutes

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

Program:

            /* ............... 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 ............... */
        

Notes:

  • 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.

Tags

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

Similar Programs Chapter Last Updated
Find Unique Elements In List Java Miscellaneous 07-10-2023
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert Date To String Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Program To Find Leap Year Between Two Dates Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
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

1 2