zhquake
zhquake

Reputation: 316

How to run task repeatedly with an increasing period in Java?

I want my task to run repeatedly, and increase the interval between two execution every time.

For example, there will be 3 seconds between the first execution and the second execution, then 5 seconds between the second and the third, then 10 between the third and the fourth...

I've checked Timer and ScheduledExecutorService and found both of them can only schedule task in a fixed period. Also I don't want to use Thread+Sleep to solve this problem, so do you have any advice? Thanks.

Upvotes: 2

Views: 133

Answers (1)

Tim B
Tim B

Reputation: 41188

You can use the ScheduledExecutorService, schedule a once-off task then in the task schedule the next one for the new delay.

Upvotes: 4

Related Questions