Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Correct usage of Swing thread?

 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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).
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50258
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren’t you starting the EDT twice? I am willing to be proved wrong.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50258
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I was obviously mistaken. Sorry
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50258
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome and sorry again for my mistake.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic