• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Correct usage of Swing thread?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79462
379
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 79462
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren’t you starting the EDT twice? I am willing to be proved wrong.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79462
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I was obviously mistaken. Sorry
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79462
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome and sorry again for my mistake.
reply
    Bookmark Topic Watch Topic
  • New Topic