Monday, March 14, 2022

[SOLVED] How do I execute a method at a particular time in java?

Issue

Is there any way to write a program in java, so that its main method schedule (or at a 10-15 min interval) another method to executes it at a particular interval?


Solution

I think you are looking for the Time class.

See Timer Class API You can use this class like:

You want to perform a Method every 600 miliseconds. You write:

ActionListener taskPerformer = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {  
                //Do your stuff
                }
            };

Timer t = new Timer(600, taskPerfomer);
t.start;

There are more options. This example will be executed once but it can be executed in an interval. I hope it helps.



Answered By - Rubinum
Answer Checked By - Cary Denson (WPSolving Admin)