Let's say I have a custom TimerTask
doing stuff.
So normally a timer task would look something like this:
public class MyTimerTask extends TimerTask {
@Override
public void run() {
// Do work...
}
}
And I schedule it for a continuous repeating cycle via a Timer
to execute every 10 seconds.
Timer myTimer = new Timer();
MyTimerTask myTask = new MyTimerTask();
myTimer.schedule(myTask,0,10000);
Under normal conditions this works just fine, but since I perform some GUI update operations in this task I have to call the Runnable
from the JavaFX application thread.
In essence this means that I have to call the Platform.runLater
on the Runnable
(MyTimerTask
) when I want to run it.
But since I don't explicitly call the run()
on the task since I put it on the TaskQueue via Timer.schedule
, I have to resort to changing MyTimerTask
to something like this:
public class MyTimerTask extends TimerTask {
@Override
public void run() {
Platform.runLater(
new Runnable() {
public void run() {
//Do work...
}
}
);
}
}
Which feels like a very crude way to do this.
So I was wondering if there is a better way to do this?