I had a bad impression (in performance) using a Swing timer. Since all events in Swing use the same thread, it seems to have quite a few delays.
I also suggest trusting your instincts about multiple instances of swinging work frames every tick. That doesn't seem right to me either. (And according to the SwingWorker , it is designed to run only once, so you cannot safely reuse it).
A timer that can do what you need is java.util.Timer . This has quite a few options for specifying timeouts, although depending on the accuracy of your time simulation, even this may not be acceptable. (For example: if you want it to run in real time, but your calculations actually take longer than in real time, what should it do? Start slowly or start skipping time steps?)
So, not knowing exactly what calc / draw is in your program, I would suggest that you try java.util.Timer . When it expires (after any time interval that you find appropriate based on your "real-time multiplier"), skip all the calculations and then drop the results back into the EDT stream to make a drawing (for example, wrapping them in SwingUtilities.invokeLater() )
Of course, this can cause locking issues if the EDT wants to reference the same objects as the computation flow. Ideally, if the downstream can pass unmanaged results to the EDT, this eliminates the need to enter locks.
(Disclaimer: none of the above takes into account several cores / processors, except one for the graphical interface, one for calculations. If you want to parallelize the application, this is probably not the right solution).
source share