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 2008

Tip: Looking for answers? Try searching our database.

Opposite of SwingUtilities.invokeLater()?

Thread view: 
larkmore@aol.com - 05 Feb 2008 20:34 GMT
So I hear a lot about how Swing isn't very thread safe.  So I'm being
very careful when a method running in a thread other than the event
dispatching thread changes my GUI widgets and wrap it in the
SwingUtilities.invokeLater() method.  So what do I do when I want to
pass information the other direction?  For example:

public class foo {
    JButton button;
    int x;
    int y;

    public foo() {
        button = new JButton("F");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Some code goes here
                x++;
                //Some other code goes here
                y++;
            }
        });
        javax.swing.Timer timer = new javax.swing.Timer(1000), new
ActionListener() {
         public void actionPerformed(ActionEvent e) {
                System.out.println(x + ", " + y);
         }
        });
        timer.start();
    }
}

So how do I make sure that both x and y are updated in one atomic (and
thread safe) operation without running the risk that the timer's
println() statement will fire in between?  If the answer involves
using synchronized methods or variables, please include some example
code as I have yet to really understand the syntax of them.  Thanks
all!
-Will
Rogan Dawes - 05 Feb 2008 20:38 GMT
> So I hear a lot about how Swing isn't very thread safe.  So I'm being
> very careful when a method running in a thread other than the event
[quoted text clipped - 34 lines]
> all!
> -Will

Since you are using the Swing Timer, you are guaranteed that the
actionPerformed method will be invoked on the EDT. Similarly, your
ActionListener for your button will only ever be invoked on the EDT.
Since they will only ever be executed by a single thread, you are
guaranteed that your Timer will never execute in between x and y.

Rogan
larkmore@aol.com - 05 Feb 2008 21:16 GMT
> Since you are using the Swing Timer, you are guaranteed that the
> actionPerformed method will be invoked on the EDT. Similarly, your
[quoted text clipped - 3 lines]
>
> Rogan

Hmm.  Good to know that the Swing Timer's actionPerformed() is always
on the EDT, but maybe I used a poor example.  I use the RXTX libraries
a lot to communicate with serial ports, and I'm worried about
asynchronous data coming from and going into those ports.  I have no
idea what thread runs when new data comes in from a serial port, but I
have a very strong hunch it won't be the EDT.  Yes/no?  What should I
do in that case, or any other case where I know for sure or at least
strongly suspect that the thread manipulating the data will not be the
EDT one?
-Will
Daniel Pitts - 05 Feb 2008 21:56 GMT
>> Since you are using the Swing Timer, you are guaranteed that the
>> actionPerformed method will be invoked on the EDT. Similarly, your
[quoted text clipped - 14 lines]
> EDT one?
> -Will
Generally, you can repeat the pattern that Swing took (BTW, you should
use EventQueue.invokeLater, not SwingUtilities)

You can create a "message queue" for your serial port, and have a single
thread that reads from that queue and writes the data to your port.  You
would have another thread who's job it is to read from and respond to
the serial data coming in.  Quite possibly that thread would pass the
actual data off to something in the event queue.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Lew - 06 Feb 2008 01:04 GMT
>>> Since you are using the Swing Timer, you are guaranteed that the
>>> actionPerformed method will be invoked on the EDT. Similarly, your
[quoted text clipped - 22 lines]
> the serial data coming in.  Quite possibly that thread would pass the
> actual data off to something in the event queue.

You can also use
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
to invoke jobs off the EDT.

Signature

Lew

Stanimir Stamenkov - 06 Feb 2008 09:55 GMT
Tue, 05 Feb 2008 13:56:55 -0800, /Daniel Pitts/:

> (BTW, you should
> use EventQueue.invokeLater, not SwingUtilities)

"Transcript of Ask the Experts session on Swing"
<http://java.sun.com/developer/community/askxprt/2006/jl1016.html>:

> *M Dunn*: Starting a Swing app. I've seen a couple of recommended
> ways:
[quoted text clipped - 22 lines]
> comes down to what you prefer. After all these years my fingers
> can't type anything but SwingUtilities. :)

Signature

Stanimir

Daniel Pitts - 06 Feb 2008 17:03 GMT
> Tue, 05 Feb 2008 13:56:55 -0800, /Daniel Pitts/:
>
[quoted text clipped - 27 lines]
>> down to what you prefer. After all these years my fingers can't type
>> anything but SwingUtilities. :)

From my understanding, SwingUtilities was a stopgap for Swing to use
before AWT implemented and used the EventQueue.  It has used the
EventQueue for long enough that you can use it yourself safely.

I wouldn't say one was so much better than the other that its worth
considerable consternation trying to figure out which one to use.  I
prefer and suggest using EventQueue, but I'm not going to search/replace
someone else codebase to match.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 16 Feb 2008 03:38 GMT
>What should I
>do in that case, or any other case where I know for sure or at least
>strongly suspect that the thread manipulating the data will not be the
>EDT one?

same as any other pair of threads.  You use locks on objects with
synchronized, volatile etc.  You read Doug Lea's book.

See http://mindprod.com/jgloss/thread.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


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.