0

Is it possible to schedule one task for different time intervals? Here is the fixed interval example:

Timer gTimer = new Timer();
Calendar startDate = Calendar.getInstance();

startDate.set(Calendar.HOUR_OF_DAY, 9);
startDate.set(Calendar.MINUTE, 45);
startDate.set(Calendar.SECOND, 00);

ScheduleClass scheduleClass = new ScheduleClass();
gTimer.schedule(scheduleClass, startDate.getTime(), 1000 * 60 * 2); //interval is 2 minutes

This snippet of the scheduleClass has the run function and it is successful to run every 2 minutes. My question is: How can I set the interval to every 2, 5 and 7 minutes?

When I copy the scheduler three times it throws this error:

SEVERE: StandardWrapper.Throwable

java.lang.IllegalStateException: Task already scheduled or cancelled`

All suggestions are welcome!

TT.
  • 15,774
  • 6
  • 47
  • 88
Ben Ao
  • 1
  • 2
  • 3
    Have a look at Quartz, it seems that it has more fine-grained scheduling options, like CRON expressions : https://quartz-scheduler.org/ – Arnaud Jan 21 '16 at 07:42
  • You can use `gTimer.schedule(scheduleClass::run, startDate.getTime(), 1000 * 60 * 2);` syntax to clone the class every time you schedule it, so it never thinks that they are the same instance – Ferrybig Jan 21 '16 at 08:43
  • `scheduleClass::run` allowed only at source level 1.8 or above, but the project is not allow to be updated. – Ben Ao Jan 22 '16 at 09:51

0 Answers0