Skip to main content

Posts

Showing posts from October, 2016

How to schedule a periodic task in Java?

To schedule a task periodically in java: 1. Create an object of the Timer class.     Timer timer = new Timer(); 2. Create an object of the TimerTask class and override the run() method of it.     TimerTask task = new TimerTask() {     @Override     public void run() {            //call your task here             }    }; 3. Call the schedule method of the Timer.     timer.schedule(task, delay, period);     Here:     a) task - task to be scheduled. Pass the task object we have created in step 2.     b) delay - delay in milliseconds before task is to be executed.     c) period - time in milliseconds between successive task executions.