While working on the animation and updating timer for a game, I read that any activities relating to the GUI should be run on the EDT, including repainting the screen. I am using a single ScheduledExecutorService
to update and draw the game (using active rendering). The initial schedule to the service (which is a nested class that implements Runnable
) is done in a call like this:
ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ex.schedule(new UpdatingService(), 1, TimeUnit.MILLISECONDS);
}
});
I thought that this would make the service run on the EDT but adding System.out.println(SwingUtilities.isEventDispatchThread());
proved that it was not.
I did some searching and found this post that mentioned starting the EDT inside the timer. I tried this and it does work. However, it doesn't seem like a good idea to nest Threads like that.
So, is nesting Threads like this not as bad as it seems? And if it is a bad idea, then what would be the proper way to ensure that a ScheduledExecutorService
is running on the EDT?