Example code from SwingWorker documentation could have race condition?

Discussion in 'Java' started by lionelv@gmail.com, Jan 31, 2007.

  1. Guest

    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.
     
    , Jan 31, 2007
    #1
    1. Advertising

  2. Guest

    On Jan 31, 4:34 pm, 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.
     
    , Jan 31, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On Jan 31, 12:07 am, wrote:
    > On Jan 31, 4:34 pm, 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.
     
    Daniel Pitts, Jan 31, 2007
    #3
  4. Guest

    On Feb 1, 8:15 am, "Daniel Pitts" <> wrote:
    > On Jan 31, 12:07 am, wrote:
    >
    > > On Jan 31, 4:34 pm, 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.


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

    Lionel.
     
    , Feb 4, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim

    race condition question

    jimjim, Nov 1, 2003, in forum: Java
    Replies:
    6
    Views:
    426
    jimjim
    Nov 2, 2003
  2. Replies:
    1
    Views:
    355
    Kevin Spencer
    Aug 7, 2006
  3. fran
    Replies:
    2
    Views:
    517
  4. techi_C
    Replies:
    2
    Views:
    1,440
    Richard Bos
    Aug 10, 2006
  5. Tim Doyle
    Replies:
    2
    Views:
    449
    Mike Treseler
    Jun 12, 2009
Loading...

Share This Page