java - Run scheduled tasks chain -
i need run scheduled tasks 1 one different delays after previous task being executed. example. there tasks list , delays list.
torun = {task1, task2, ..., taskn} delays = {100, 9, 22, ..., 1000}
now need run task1 throgh 100ms, task2 after task1 through 9ms, task3 after task2 through 22ms , on.
i using javafx. task can use ui update methods, changing nodes positions. forces me use platform.runlater() method, because if don't, have exception "not on fx application thread". know method runs runnable object after undefined amount of time. , depending of how time can vary have 2 ways.
2.1 continue use platform.runlater() method if call time < 1-2ms.
2.2 find solution, don't have yet.
shortly actual task. record user actions mouse move, mouse click, , application events. after need replay actions. go through every action using scheduledexecutorservice.schedule(), , inside of every action task put next task scheduler. , goes wrong every time, or not feels stable enough.
i think code here not useful, because 2 messy , little bit big quick figuring out
assuming actual tasks themselves, not including delay, not take long time run (so may executed in entirety on fx application thread), can use timeline
follows:
runnable[] tasks = { /* task1, task2, ..., taskn */ } ; // n elements int[] delays = { 100, 9, 22, ..., 1000 }; // n-1 elements (delay after task1, ..., task(n-1) keyframe[] frames = new keyframe[tasks.length] ; int cumulativemillis = 0 ; (int = 0; < tasks.length; i++) { frames[i] = new keyframe(duration.millis(cumulativemillis), event -> tasks[i].run()); if (i < delays.length) { cumulativemillis += delays[i] ; } } timeline timeline = new timeline(frames); timeline.play();
with approach, there no need worry multithreading: timeline
takes care of threading issues you. each task executed on fx application thread (but pauses between them not block thread).
Comments
Post a Comment