开发者

Timer with fixed delays between Task

开发者 https://www.devze.com 2023-03-11 06:40 出处:网络
I want to have a timer task that runs two tasks with fixed delays between each task. For example: A------B-----A------B

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

精彩评论

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

关注公众号