Example code from SwingWorker documentation could have race condition?



I refer to the documentation of the get() method in SwingWorker found
at https://swingworker.dev.java.net/javadoc/org/jdesktop/swingworker/
SwingWorker.html#get()

It includes the following example code as a suggestion when you want
to block and wait for the SwingWorker to finish. I've put some numbers
in for easier explanation.

class SwingWorkerCompletionWaiter extends PropertyChangeListener {
private JDialog dialog;

public SwingWorkerCompletionWaiter(JDialog dialog) {
this.dialog = dialog;
}

4 public void propertyChange(PropertyChangeEvent event) {
5 if ("state".equals(event.getPropertyName())
6 && SwingWorker.StateValue.DONE ==
event.getNewValue()) {
7 dialog.setVisible(false);
8 dialog.dispose();
}
}
}
1 JDialog dialog = new JDialog(owner, true);
2 swingWorker.addPropertyChangeListener(
new SwingWorkerCompletionWaiter(dialog));
3 swingWorker.execute();
//the dialog will be visible until the SwingWorker is done
9 dialog.setVisible(true);

Is the execution path according to the numbers I've inserted above
possible? I would have thought that swingWorker.execute() could
potentially run the worker thread so quickly that it executes 8 before
9.

Would it not then block at 9?

Clarification would be great. I suspect there is something I'm missing
about what happens in SwingWorker.

Lionel.

.



Relevant Pages

  • Re: JDialog displaying progress
    ... I'm trying to display on modal JDialog progress of copying files. ... used SwingWorker for copying the files, so it won't freeze the JDialog, ... public void run{ ...
    (comp.lang.java.programmer)
  • JDialog displaying progress
    ... I'm trying to display on modal JDialog progress of copying files. ... used SwingWorker for copying the files, so it won't freeze the JDialog, ... public void run{ ...
    (comp.lang.java.programmer)