I have read this thread and verified that it results in a deadlock (JavaSE-22). However, I simply cannot figure out why. Can anyone kindly explain what exactly happens? I know that we are NOT supposed to do it, but I wish to understand what goes wrong.
I've checked that the constructor actually finished running, and yet text.setText()
gets blocked on something. It appears that either of window.pack()
or window.setVisible(true)
suffice to cause the deadlock. I experimented with wrapping one or both of add(text)
and updateGui();
in a SwingUtilities.invokeLater
, and it only works when both are wrapped. Of course, in doing so the window.pack();
fails to do what we want, but right now I'm just trying to understand why there is a deadlock in the first place. My expertise is in multithreaded programming and I know very well how deadlocks in general happen, but cannot understand what Swing is doing here. What are pack
and setVisible
and setText
doing that together contribute to the deadlock? I can think of a hundred and one ways, but none of them really make sense to me because I don't see why it should be designed that way at all!