Skip to content
Advertisement

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

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement