0

I want to schedule a task in java which runs in every 2 min. The task should run on every 2 min even though previous task is not yet finished. (I.e. next task executing parallel to previous incomplete task.) Is there any way of doing this in java or in spring?

Insane Skull
  • 8,810
  • 9
  • 38
  • 59
user4325796
  • 75
  • 1
  • 8
  • You can use TimerTask. Quartz scheduler is also an option. ScheduledThreadPoolExecutor is not an option since you want the next task to run even if the previous task has not finished. – akki Dec 14 '15 at 09:28
  • Thanks for your suggestion – user4325796 Dec 14 '15 at 13:13

2 Answers2

0

You could use the Quartz Scheduler for this. It allows you to schedule a task to run at an interval (in miliseconds) or on a predefined date and time (for as long as specified) based on a CRON expression.

You can find the documentation for this here: https://quartz-scheduler.org/documentation

If Quartz isn't an option, there is a long, comprehensive list of alternatives at java-source.net on "Open Source Job Schedulers in Java" :

http://java-source.net/open-source/job-schedulers

Community
  • 1
  • 1
Smittey
  • 2,410
  • 10
  • 28
  • 33
0

Sounds to me like you are looking for a ScheduledExecutorService.

An ExecutorService that can schedule commands to run after a given delay, or to execute periodically.

The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.

Community
  • 1
  • 1
OldCurmudgeon
  • 60,862
  • 15
  • 108
  • 197
  • Hi, scheduleAtFixedRate java doc itself saying that next task might delay depending upon execution of previous task but both cannot run in concurrent way. – user4325796 Dec 14 '15 at 13:15