Example code from SwingWorker documentation could have race condition?

L

lionelv

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.
 
L

lionelv

On Jan 31, 4:34 pm, (e-mail address removed) wrote:
[snip]

I suspect I may have figured it out. Because swingWorker.execute() is
called from the EDT this thread is already executing, so any other
actions that are requested to execute must do so after this has
finished, thus dialog.setVisible(true) will always occur first.

Cany anyone confirm that? I figured it out after sticking in some
sleep times in there.

Lionel.
 
D

Daniel Pitts

On Jan 31, 4:34 pm, (e-mail address removed) wrote:
[snip]

I suspect I may have figured it out. Because swingWorker.execute() is
called from the EDT this thread is already executing, so any other
actions that are requested to execute must do so after this has
finished, thus dialog.setVisible(true) will always occur first.

Cany anyone confirm that? I figured it out after sticking in some
sleep times in there.

Lionel.


I would guess you are right.
The creation of the JDialog box, and the following setVisible(true)
will all be executed in the EDT. "uninterrupted" by other EDT tasks.
 
L

lionelv

On Jan 31, 4:34 pm, (e-mail address removed) wrote:
[snip]
I suspect I may have figured it out. Because swingWorker.execute() is
called from the EDT this thread is already executing, so any other
actions that are requested to execute must do so after this has
finished, thus dialog.setVisible(true) will always occur first.
Cany anyone confirm that? I figured it out after sticking in some
sleep times in there.

I would guess you are right.
The creation of the JDialog box, and the following setVisible(true)
will all be executed in the EDT. "uninterrupted" by other EDT tasks.

Thanks. I'm glad you understood what I said because reading back I
wouldn't have :).

Lionel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top