I'm getting a deadlock in the Swing application I maintain, and although I have a workaround that appears to work, I'm not sure that I have understood what I am doing and haven't just hidden a race condition that may pop up again later.
A thread trace shows the deadlock is occurring between two threads, AWT-EventQueue-0 and AWT-EventQueue-1. My first question is which if either of these is the infamous Event Dispatching Thread. Both threads have the following at the bottom of their stack trace:
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
I think the root of the problem is that the application classes mix domain data with graphical components, and in this case both threads are trying to lock both a java.awt.Component$AWTTreeLock
and one of my own objects (say X). My workaround is to use SwingUtilities.invokeLater()
in one place where X is locked, even though this is already on the EDT. According to the Javadoc this means the call "deferred until all pending events have been processed". However, I'm not quite sure that this is really a solution, and in any case I'm unclear why there seem to be two EDTs.
Can anyone explain what is going on? I can try to provide a cut-down version of the code but it may take me a while to edit out the irrelevant complications.