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 / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Thread Safety with SwingWorker

Thread view: 
Rob - 16 Feb 2005 09:18 GMT
Hi,

I have a class that extends a jframe with some additional private
fields. It also has an inner class that extends SwingWorker and some
inner classes that are action listeners.

I was wondering if it is safe to write to a (private) field from the
SwingWorker finished method, while the listeners read the private field.

example:

public class MyDialog extends JDialog {

public MyDialog(Dialog owner) {
    super(owner);
    closeButton.addActionListener(new CloseListener());
    workButton.addActionListener(new WorkListener());
}

private MyWorker worker;

    class CloseListener implements ActionListener {

        public void actionPerformed(ActionEvent e)
        {
            if (worker != null)
                return;
            hide();
        }

    }

    class WorkListener implements ActionListener {

        public void actionPerformed(ActionEvent e)
        {
            if (worker != null)
                return;

            worker = new MyWorker(tree);
            worker.start();
        }

    }

    class MyWorker extends SwingWorker
    {

        public Object construct()
        {
            // working hard
            return null;
        }

        public void finished()
        {
            worker = null;
        }

}

I would also appreciate any resources, such as URL's, about using the
SwingWorker in combination with progress and interrupt.
Thanks for any help!
xarax - 16 Feb 2005 15:19 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> I was wondering if it is safe to write to a (private) field from the
> SwingWorker finished method, while the listeners read the private field.

IIRC, the finished() method executes in the AWT EDT, so it
is safe, because the listeners also run in the AWT EDT.

Otherwise, you would have to surround the accesses/modifies
with a synchronized{} block to force cache flushing to main
memory, or maybe use the volatile keyword.

> example:
>
[quoted text clipped - 51 lines]
> SwingWorker in combination with progress and interrupt.
> Thanks for any help!
Rob - 17 Feb 2005 08:35 GMT
>>Hi,
>>
[quoted text clipped - 7 lines]
> IIRC, the finished() method executes in the AWT EDT, so it
> is safe, because the listeners also run in the AWT EDT.

This is what I was thinking too.
Thanks. I hope you remember correctly :)


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



©2008 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.