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----30I 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------20How 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.
精彩评论