This discussion is archived
0 Replies Latest reply: Nov 19, 2007 6:52 PM by 843807 RSS

Race condition in EventQueueMonitor

843807 Newbie
Currently Being Moderated
It looks like the example code to add listeners to EventQueueMonitor contains a race condition. The example code presented just about everywhere looks like:

...
if (EventQueueMonitor.isGUIInitialized()) {
createGUI();
}
else {
EventQueueMonitor.addGUIInitializedListener(this);
}
...

public void guiInitialized() {
createGUI();
}

public void createGUI() {
SwingEventMonitor.addMouseMotionListener(this);     
SwingEventMonitor.addMouseListener(this);
}

If the top snipped is close enough to the end of the buildup of the GUI, sometimes it works sometimes it does not.
When the isGuiInitialized flag is checked, it is false, so the execution goes on the 'else' branch. But before the code is executed, the initialization finishes and the flag changes to 'true'. So, by the time the addGUIInitializedListener(this) is executed, the event is gone and the guiInitialized() method is never called.

Has anyone come across this problem?