{"id":5559,"date":"2023-01-06T07:21:55","date_gmt":"2023-01-06T07:21:55","guid":{"rendered":"https:\/\/tuvoc.com\/?p=5559"},"modified":"2024-11-15T12:15:43","modified_gmt":"2024-11-15T12:15:43","slug":"how-to-schedule-recurring-tasks-in-java-applications","status":"publish","type":"post","link":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/","title":{"rendered":"How to Schedule Recurring Tasks in Java Applications?"},"content":{"rendered":"<p>With classes like java.until.Timer and java.until.TimerTask, called Java Timer framework, you can set up the scheduling of simple tasks in J2ME. Before that, the developers had to write their own scheduler with object.wait() method.<\/p>\n<p>Today, we will understand how to schedule recurring tasks in Java. Timer and TimerTask frameworks can be used to allow flexible scheduling. It is easy to learn and consists of two classes.<\/p>\n<p>Before we start scheduling a recurring task, let&#8217;s understand the one-shot task first.<\/p>\n<h2><span style=\"font-weight: 600;\">Scheduling a One-Time Task for a Timer<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Here, we first need to look at framework classes since the scheduling framework is built on top of these framework classes. Let&#8217;s understand how a scheduling framework is used and implemented with an example of a simple timer for boiling water.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Imagine a timer that tells you how many minutes your water has been eating and plays a sound when it reaches a certain number. Here is how you code it:<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling.example;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Timer;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.TimerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class WaterTimer {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final Timer timer = new Timer();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final int minutes;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public WaterTimer(int minutes) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.minutes = minutes;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void start() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timer.schedule(new TimerTask() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0playSound();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timer.cancel();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private void playSound() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Your water is boiling!\");<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Start a new thread to play a sound...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}, minutes \u2217 60 \u2217 1000);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WaterTimer waterTimer = new waterTimer(2);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0waterTimer.start();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Here, the WaterTimer instance owns the Timer instance that will provide necessary scheduling. When the timer is started with start(), the TimerTask will be scheduled to execute after a specific time. When it reaches that time, run() will be called to TimerTask by Timer and it will play the sound. The application will be terminated after the timer is canceled.<\/span><\/p>\n<h3><span style=\"font-weight: 600;\">Scheduling Recurring Tasks<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">&#8216;Timer&#8217; will allow the tasks to be scheduled for repeated execution by defining a fixed rate or delay between two recurrences. Your application may have a little more scheduling requirements than that, though.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s take an alarm clock as an example. If a country or timezone uses daylight saving time, you cannot schedule the Timer for 86400000 milliseconds (24 hours) as it will make your alarm too late or too early when the Daylight saving time will be in effect.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, you will have to use calendar arithmetic to set the next occurrence for your alarm to go off. Here, we will use the AlarmClock class. You can easily find the source code for the same online.<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling.example;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.text.SimpleDateFormat;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Date;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import org.tiling.scheduling.Scheduler;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import org.tiling.scheduling.SchedulerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import org.tiling.scheduling.example.iterators.DailyIterator;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class AlarmClock {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final Scheduler scheduler = new Scheduler();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final SimpleDateFormat dateFormat =<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new SimpleDateFormat(\"dd MMM yyyy HH:mm:ss.SSS\");<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final int hourOfDay, minute, second;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public AlarmClock(int hourOfDay, int minute, int second) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.hourOfDay = hourOfDay;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.minute = minute;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.second = second;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void start() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0scheduler.schedule(new SchedulerTask() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0soundAlarm();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private void soundAlarm() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Time to wake up, \" +<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"It's \" + dateFormat.format(new Date()));<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Start a new thread to sound an alarm...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}, new DailyIterator(hourOfDay, minute, second));<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AlarmClock alarmClock = new AlarmClock(7, 0, 0);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0alarmClock.start();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">The code is pretty similar to our Water timer applications. The AlarmClock owns the Scheduler instance to provide the scheduling.\u00a0 With start(), SchedulerTask is scheduled by the alarm clock to play the alarm.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Instead of using the delay of 24 hours between alarms (because of our daylight saving time issue), we have used DailyIterator to define the schedule. This means the task will be executed at 7 in the morning every day.<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">Time to wake up, It's 01 Dec 2022 07:00:00.003<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Time to wake up, It's 02 Dec 2022 07:00:00.002<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Time to wake up, It's 03 Dec 2022 07:00:00.027<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Time to wake up, It's 04 Dec 2022 07:00:00.016<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u2026<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">The interface ScheduleIterator is implemented by DailyIterator. It specifies the scheduled time of execution of SchedulerTask as a series of java.until.date objects. next() will iterate Date objects in chronological order and the return value of null will cancel the task. A \u2018reschedule\u2019 attempt will show an exception. Here is the ScheduleIterator interface:<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.until.Date;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public interface ScheduleIterator {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public Date next();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">The next() method of DailyIterator will return a Date object at the same time every day. If you call next() on a newly constructed DailyIterator, you will get 7:00 AM on subsequent days, forever.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here, DailyIterator is using java.until.Calendar instance. The next() will return the correct date by adding a day to the calendar. The &#8216;Calendar&#8217; implementation takes care of daylight saving time, and it doesn&#8217;t need to be added by the developer.<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling.examples.iterators;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import org.tiling.scheduling.ScheduleIterator;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Calendar;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Date;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\u2217\u2217<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u2217 A DailyIterator class returns a sequence of dates on subsequent days<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u2217 representing the same time each day.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u2217\/<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class DailyIterator implements ScheduleIterator {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final int hourOfDay, minute, second;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final Calendar calendar = Calendar.getInstance();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public DailyIterator(int hourOfDay, int minute, int second) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this(hourOfDay, minute, second, new Date());<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public DailyIterator(int hourOfDay, int minute, int second, Date date) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.hourOfDay = hourOfDay;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.minute = minute;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.second = second;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.setTime(date);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.set(Calendar.MINUTE, minute);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.set(Calendar.SECOND, second);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.set(Calendar.MILLISECOND, 0);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (!calendar.getTime().before(date)) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.add(Calendar.DATE, \u20111);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public Date next() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0calendar.add(Calendar.DATE, 1);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return calendar.getTime();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<h3><span style=\"font-weight: 600;\">Scheduling Framework Implementation<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Scheduler and SchedulerTask are the two other classes apart from ScheduleIterator to make up the framework. They use Timer and TimerTasks, counting schedules as a series of one-shot timers.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Scheduler source code:<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Date;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.Timer;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.TimerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class Scheduler {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0class SchedulerTimerTask extends TimerTask {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private SchedulerTask schedulerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private ScheduleIterator iterator;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public SchedulerTimerTask(SchedulerTask schedulerTask,<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ScheduleIterator iterator) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.schedulerTask = schedulerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.iterator = iterator;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void run() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.run();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0reschedule(schedulerTask, iterator);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private final Timer timer = new Timer();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public Scheduler() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void cancel() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timer.cancel();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public void schedule(SchedulerTask schedulerTask,<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ScheduleIterator iterator) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Date time = iterator.next();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (time == null) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.cancel();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized(schedulerTask.lock) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (schedulerTask.state != SchedulerTask.VIRGIN) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new IllegalStateException(\"Task already\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0scheduled \" + \"or cancelled\");<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.state = SchedulerTask.SCHEDULED;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.timerTask =<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new SchedulerTimerTask(schedulerTask, iterator);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timer.schedule(schedulerTask.timerTask, time);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private void reschedule(SchedulerTask schedulerTask,<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ScheduleIterator iterator) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Date time = iterator.next();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (time == null) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.cancel();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized(schedulerTask.lock) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (schedulerTask.state != SchedulerTask.CANCELLED) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0schedulerTask.timerTask =<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0new SchedulerTimerTask(schedulerTask, iterator);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timer.schedule(schedulerTask.timerTask, time);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">SchedulerTask source code:<\/span><\/p>\n<pre>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><span style=\"font-weight: 400;\">package org.tiling.scheduling;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">import java.util.TimerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public abstract class SchedulerTask implements Runnable {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0final Object lock = new Object();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int state = VIRGIN;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static final int VIRGIN = 0;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static final int SCHEDULED = 1;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static final int CANCELLED = 2;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0TimerTask timerTask;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0protected SchedulerTask() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public abstract void run();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public boolean cancel() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized(lock) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (timerTask != null) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0timerTask.cancel();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0boolean result = (state == SCHEDULED);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0state = CANCELLED;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return result;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public long scheduledExecutionTime() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0synchronized(lock) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return timerTask == null ? 0 : timerTask.scheduledExecutionTime();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Like our Water Timer, every instance of Scheduler owns an instance of Timer, which provides underlying scheduling. The single one-shot timer for the water timer is replaced by the \u2018Scheduler\u2019 that strings a chain of one-shot timers. This executes SchedulerTask at the time defined by the ScheduleIterator.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The first execution of SchedulerTask is discovered by calling next() on the interface &#8216;ScheduleIterator&#8217;. The scheduling takes place after calling a one-shot schedule() on the underlying Timer class. TimerTask is supplied to one-shot execution and the nested ScheduleTimerTask contains the task and the iterator.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The run() will be called at the allotted time on the nested class, where the packaged task and iterators are referenced to reschedule the next occurrence of the task.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Reschedule() is similar to schedule(), but it is private and there are different checks on SchedulerTask. The rescheduling will process the recurrence indefinitely. That will construct a new nested class instance for each scheduled execution until the scheduler or the task is canceled.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SchedulerTask is like TimerTask. When created, the state is VIRGIN (never been scheduled). It shifts to SCHEDULED states once scheduled and then to CANCELLED after it&#8217;s canceled.<\/span><\/p>\n<h3><span style=\"font-weight: 600;\">Canceling The Tasks<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The three ways to cancel scheduling tasks are as follows.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Calling the cancel() method on SchedulerTask. It is the same as cancel() on TimerTask the task will never run again, but it will run to completion if already running.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The second way is with ScheduleIterator to return null. This is a shortcut to the first way. The scheduler class will call cancel() on the SchedulerTask. This will help you control when the scheduling stops as it will stop the iterator rather than the task.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The third will be to cancel the whole Scheduler by calling its cancel() method. This will cancel all of the tasks of the scheduler and no more tasks are scheduled on it.<\/span><\/p>\n<h3><span style=\"font-weight: 600;\">Conclusion<\/span><\/h3>\n<p>Here, we have discussed scheduling one-time and recurring tasks in Java applications and how to cancel them. It uses the Java Timer framework with the object.wait() method and Timer, scheduler, and schedulerTask classes to set up recurring tasks. Further, we have also discussed the three ways to use cancel() to cancel the scheduled task when you want to stop executing them, which is an essential aspect of <strong><a href=\"https:\/\/www.tuvoc.com\/technologies\/backend\/java-development-service\/\">Java development services<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With classes like java.until.Timer and java.until.TimerTask, called Java Timer framework, you can set up the scheduling of simple tasks in J2ME. Before that, the developers had to write their own scheduler with object.wait() method. Today, we will understand how to schedule recurring tasks in Java. Timer and TimerTask frameworks can be used to allow flexible [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5563,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[124],"tags":[],"class_list":["post-5559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scheduling Recurring Tasks in Java Applications<\/title>\n<meta name=\"description\" content=\"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scheduling Recurring Tasks in Java Applications\" \/>\n<meta property=\"og:description\" content=\"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\" \/>\n<meta property=\"og:site_name\" content=\"Tuvoc Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/tuvoctechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T07:21:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-15T12:15:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png\" \/>\n\t<meta property=\"og:image:width\" content=\"868\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Jitendra Rathod\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Tuvocpvtltd\" \/>\n<meta name=\"twitter:site\" content=\"@Tuvocpvtltd\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jitendra Rathod\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\"},\"author\":{\"name\":\"Jitendra Rathod\",\"@id\":\"https:\/\/www.tuvoc.com\/#\/schema\/person\/2a427deaad57e0f7ff9dbb1f14cdb7d9\"},\"headline\":\"How to Schedule Recurring Tasks in Java Applications?\",\"datePublished\":\"2023-01-06T07:21:55+00:00\",\"dateModified\":\"2024-11-15T12:15:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\"},\"wordCount\":971,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.tuvoc.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png\",\"articleSection\":[\"App Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\",\"url\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\",\"name\":\"Scheduling Recurring Tasks in Java Applications\",\"isPartOf\":{\"@id\":\"https:\/\/www.tuvoc.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png\",\"datePublished\":\"2023-01-06T07:21:55+00:00\",\"dateModified\":\"2024-11-15T12:15:43+00:00\",\"description\":\"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage\",\"url\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png\",\"contentUrl\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png\",\"width\":868,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.tuvoc.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Schedule Recurring Tasks in Java Applications?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tuvoc.com\/#website\",\"url\":\"https:\/\/www.tuvoc.com\/\",\"name\":\"Tuvoc Technologies\",\"description\":\"Top Secure Web &amp; Mobile Application Development Company\",\"publisher\":{\"@id\":\"https:\/\/www.tuvoc.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.tuvoc.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.tuvoc.com\/#organization\",\"name\":\"Tuvoc Technologies\",\"url\":\"https:\/\/www.tuvoc.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.tuvoc.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/07\/Tuvoc-1.svg\",\"contentUrl\":\"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/07\/Tuvoc-1.svg\",\"width\":1,\"height\":1,\"caption\":\"Tuvoc Technologies\"},\"image\":{\"@id\":\"https:\/\/www.tuvoc.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/tuvoctechnologies\/\",\"https:\/\/x.com\/Tuvocpvtltd\",\"https:\/\/www.instagram.com\/tuvocpvtltd\/\",\"https:\/\/www.linkedin.com\/company\/tuvoc-technologies\/people\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.tuvoc.com\/#\/schema\/person\/2a427deaad57e0f7ff9dbb1f14cdb7d9\",\"name\":\"Jitendra Rathod\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.tuvoc.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d373e90892c0a256ef4c451874d1bd84?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d373e90892c0a256ef4c451874d1bd84?s=96&d=mm&r=g\",\"caption\":\"Jitendra Rathod\"},\"url\":\"https:\/\/www.tuvoc.com\/author\/jitendra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scheduling Recurring Tasks in Java Applications","description":"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/","og_locale":"en_US","og_type":"article","og_title":"Scheduling Recurring Tasks in Java Applications","og_description":"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.","og_url":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/","og_site_name":"Tuvoc Technologies","article_publisher":"https:\/\/www.facebook.com\/tuvoctechnologies\/","article_published_time":"2023-01-06T07:21:55+00:00","article_modified_time":"2024-11-15T12:15:43+00:00","og_image":[{"width":868,"height":540,"url":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png","type":"image\/png"}],"author":"Jitendra Rathod","twitter_card":"summary_large_image","twitter_creator":"@Tuvocpvtltd","twitter_site":"@Tuvocpvtltd","twitter_misc":{"Written by":"Jitendra Rathod","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#article","isPartOf":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/"},"author":{"name":"Jitendra Rathod","@id":"https:\/\/www.tuvoc.com\/#\/schema\/person\/2a427deaad57e0f7ff9dbb1f14cdb7d9"},"headline":"How to Schedule Recurring Tasks in Java Applications?","datePublished":"2023-01-06T07:21:55+00:00","dateModified":"2024-11-15T12:15:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/"},"wordCount":971,"commentCount":0,"publisher":{"@id":"https:\/\/www.tuvoc.com\/#organization"},"image":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png","articleSection":["App Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/","url":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/","name":"Scheduling Recurring Tasks in Java Applications","isPartOf":{"@id":"https:\/\/www.tuvoc.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage"},"image":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png","datePublished":"2023-01-06T07:21:55+00:00","dateModified":"2024-11-15T12:15:43+00:00","description":"Learn to schedule and manage recurring tasks in Java applications using the Java Timer framework, with tips on effective task cancellation.","breadcrumb":{"@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#primaryimage","url":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png","contentUrl":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/01\/How-to-Schedule-Recurring-Tasks-in-Java-Applications.png","width":868,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/www.tuvoc.com\/blog\/how-to-schedule-recurring-tasks-in-java-applications\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.tuvoc.com\/"},{"@type":"ListItem","position":2,"name":"How to Schedule Recurring Tasks in Java Applications?"}]},{"@type":"WebSite","@id":"https:\/\/www.tuvoc.com\/#website","url":"https:\/\/www.tuvoc.com\/","name":"Tuvoc Technologies","description":"Top Secure Web &amp; Mobile Application Development Company","publisher":{"@id":"https:\/\/www.tuvoc.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.tuvoc.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.tuvoc.com\/#organization","name":"Tuvoc Technologies","url":"https:\/\/www.tuvoc.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.tuvoc.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/07\/Tuvoc-1.svg","contentUrl":"https:\/\/www.tuvoc.com\/wp-content\/uploads\/2023\/07\/Tuvoc-1.svg","width":1,"height":1,"caption":"Tuvoc Technologies"},"image":{"@id":"https:\/\/www.tuvoc.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/tuvoctechnologies\/","https:\/\/x.com\/Tuvocpvtltd","https:\/\/www.instagram.com\/tuvocpvtltd\/","https:\/\/www.linkedin.com\/company\/tuvoc-technologies\/people\/"]},{"@type":"Person","@id":"https:\/\/www.tuvoc.com\/#\/schema\/person\/2a427deaad57e0f7ff9dbb1f14cdb7d9","name":"Jitendra Rathod","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.tuvoc.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d373e90892c0a256ef4c451874d1bd84?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d373e90892c0a256ef4c451874d1bd84?s=96&d=mm&r=g","caption":"Jitendra Rathod"},"url":"https:\/\/www.tuvoc.com\/author\/jitendra\/"}]}},"_links":{"self":[{"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/posts\/5559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/comments?post=5559"}],"version-history":[{"count":0,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/posts\/5559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/media\/5563"}],"wp:attachment":[{"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/media?parent=5559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/categories?post=5559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tuvoc.com\/wp-json\/wp\/v2\/tags?post=5559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}