Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Example code from SwingWorker documentation could have race condition?

Thread view: 
lionelv@gmail.com - 31 Jan 2007 06:34 GMT
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.
lionelv@gmail.com - 31 Jan 2007 08:07 GMT
On Jan 31, 4:34 pm, lion...@gmail.com 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.
Daniel Pitts - 31 Jan 2007 22:15 GMT
On Jan 31, 12:07 am, lion...@gmail.com wrote:
> On Jan 31, 4:34 pm, lion...@gmail.com wrote:
> [snip]
[quoted text clipped - 8 lines]
>
> 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.
lionelv@gmail.com - 04 Feb 2007 07:27 GMT
> On Jan 31, 12:07 am, lion...@gmail.com wrote:
>
[quoted text clipped - 14 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.