Can deadlock happen for an arbitrary synchronised method which updates some Swing components if the threads which call to this method all use Swing Timer or SwingUtilities.invokeLater()
. I think that it will not be necessary to call invokeLater() inside Swing Timer tasks.
So I will have Some Swing timers and some observers that update my components. Observers all call invokeLater()
and Swing Timers call my arbitrary method updateComponents()
directly.
If it can still cause Deadlock, will invokeLater()
make me sure that no deadlock will happen?
Actions triggered by the Swing Timer, as well as runnables submitted to
invokeLater
, are all executed sequentially on the Event Dispatch Thread, and as long as you don't involveinvokeAndWait
, no other thread is waiting for an action to complete. Therefore no deadlock can arise from this usage pattern alone. If you do encounter a deadlock, the cause will not be related to the EDT and its event processing.You do not need
invokeLater
from within any event callback, which includes the one submitted to the Swing Timer, because they are already guaranteed to be called on the Event Dispatch Thread.