File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Correct usage of Swing thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Correct usage of Swing thread?" Watch "Correct usage of Swing thread?" New topic
Author

Correct usage of Swing thread?

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I have a JPanel with data processing responsibilities.



I first launch a non-swing thread in the JPanel's constructor. Then the non-swing thread takes its time performing data processing, and if it needs to update the GUI, it adds a Runnable to the AWT queue.

Is this the correct way of using the Swing thread?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Mikpo Siru wrote: . . .I have a JPanel with data processing responsibilities.
That sounds like dubious design. Your panel is for display, and you should do the data processing in another class
. . .

Is this the correct way of using the Swing thread?

Thanks
No. You start the thread (called EDT) once with invokeLater or similar, and use that for all Swing-related activities. If you need another thread for a process of long duration, you start another thread for that. It is simpler to use a SwingWorker object, which will do the threading for you.

If you follow that link back, you find it is part of the Swing concurrency “trail”. There are other links in the SwingUtilities#invokeLater() method and the Swing summary (scroll down to the bottom and look for other links).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Campbell Ritchie wrote:You start the thread (called EDT) once with invokeLater or similar, and use that for all Swing-related activities. If you need another thread for a process of long duration, you start another thread for that.

Isn't that what's going on? The JPanel is created on the EDT. This creates and starts a new thread. Inside this new thread, an update is made to the GUI on the EDT (invokeLater). The new thread then does the long duration process. Afterwards, it updates the GUI on the EDT again (invokeLater).
But yes, SwingWorker can make it easier.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Aren’t you starting the EDT twice? I am willing to be proved wrong.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

No, invokeLater(...) / invokeAndWait(...) or indeed constructing any Swing component will start the EDT only if it's not already running.

Interestingly, start()ing a Swing Timer doesn't launch the EDT until the Timer fires it's ActionListener's actionPerformed(...) so if the EDT hasn't been started and the main thread terminates, a Timer doesn't fire at all. Once the EDT is started, though, a Timer can keep the program alive ad infinitum.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Thank you. I was obviously mistaken. Sorry
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Campbell Ritchie wrote:It is simpler to use a SwingWorker object, which will do the threading for you.

If you follow that link back, you find it is part of the Swing concurrency “trail”. There are other links in the SwingUtilities#invokeLater() method and the Swing summary (scroll down to the bottom and look for other links).

Thanks, I'll take a look at SwingWorker.

Thanks everyone for your input.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
You’re welcome and sorry again for my mistake.
 
 
subject: Correct usage of Swing thread?