Threading and deadlock in a Swing application

I get a dead end in the Swing application that I support, and although I have a workaround that seems to work, I'm not sure that I understood what I was doing and didnโ€™t just hide the race status, it might appear later.

A thread trace indicates that a deadlock occurs between the two AWT-EventQueue-0 and AWT-EventQueue-1 threads. My first question is which one is a shameful Dispatching Thread event. Both threads have the bottom of the stack trace:

at java.awt.EventDispatchThread.run(EventDispatchThread.java:138) 

I think the root of the problem is that application classes mix domain data with graphical components, in which case both threads try to block both 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, although this is already on EDT. According to Javadoc, this means that the call is โ€œdeferred until all incomplete events are processed.โ€ However, I'm not quite sure if this is really a solution, and in any case, I don't understand why there seem to be two EDTs.

Can anyone explain what is happening? I can try to provide an abbreviated version of the code, but it may take me some time to edit the unulocal complications.

+2
source share
2 answers

Thank you Ishay for pointing me in the right direction. An application creates its own subclass of java.awt.EventQueue and uses Toolkit.getDefaultToolkit().getSystemEventQueue().push(newQueue) to replace the original queue. The original queue should still process the task in its thread, AWT-EventQueue-0 , while events begin to arrive in a new queue in the AWT-EventQueue-1 thread, which leads to a deadlock.

+1
source

Source: https://habr.com/ru/post/1483399/


All Articles