Hank
Hank

Reputation: 3497

Timer with fixed delays between Task

I want to have a timer task that runs two tasks with fixed delays between each task.

For example:

A------B-----A------B
0------10----20----30

I tried using this code:

timer.scheduleAtFixedRate(taskA, 0, 10000);
timer.scheduleAtFixedRate(taskB, 10000, 10000);

but that gives me:

A-----A,B-----A,B
0-----10------20

How do I do this using Timer and TimerTask?

Upvotes: 0

Views: 201

Answers (3)

toto2
toto2

Reputation: 5326

Just double your interval:

  timer.scheduleAtFixedRate(taskA, 0, 20000);
  timer.scheduleAtFixedRate(taskB, 10000, 20000);

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234795

I would do this with a single Task that maintains an internal toggle.

Upvotes: 1

jzd
jzd

Reputation: 23629

Create one Timer and have it alternate the the task it calls. Or create two Timers that one for Task A and one for Task B that have double the delay.

Upvotes: 1

Related Questions