开发者

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(task开发者_C百科B, 10000, 10000);

but that gives me:

A-----A,B-----A,B

0-----10------20

How do I do this using Timer and TimerTask?


Just double your interval:

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜