0

While scheduling tasks using java.util.TimerTask how i can make sure that run method will execute only after current execution is completed, otherwise tasks queue size will keep growing and eventually task will be executing always. i am beginner and looking help

Pirai Sudie
  • 162
  • 3
  • 17

2 Answers2

1

Use ExecutorService#scheduleWithFixedDelay(). This will start the 'delay' when the current task finishes (as opposed to scheduleAtFixedRate())

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
1

Use a java.util.Timer with the TimerTask. One of these timer's two methods can be used:

  • schedule(TimerTask task, long delay, long period)

  • scheduleAtFixedRate(TimerTask task, long delay, long period)

Where:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions.

Also, refer this article: What is the difference between schedule and scheduleAtFixedRate?

prasad_
  • 12,755
  • 2
  • 24
  • 36