Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Filthy Rich Clients: Swiing and threads  RSS feed

 
ludoviko azuaje
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hy,

As we know Swing is not multithreaded.

Does your book recommend techniques for overcoming this limitation of Swing?

thanks.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the single-threadedness is a slight limitation, it is one shared by just about all GUI toolkits. Why do you think it needs to be "overcome"?
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is by design to allow UI and models to be unsynchronized by default, and you can use various multi-threading trickeries to support doing multiple things at the same time, namely to retain UI control and having some work done in the background.

However, the lack of communication between the UI thread and spawned worker threads can be difficult to tackle, such as when a worker thread finishes its task, the UI is not in the right/expected state, such as the user has switched to another panel, and cannot accept the output of the job.

I am wondering if the authors of FRC finds this a problem that needs to be addressed. Suppose a user clicks on something to spawn some fancy animations, and before the animation is completed, the user clicks on a tab to switch away, potentially causing NPE or unexpected exceptions.
[ September 18, 2007: Message edited by: Chu Tan ]
 
Romain Guy
author
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After having written tons of animations I never ran into serious problems. However, Timing Framework is built so that animation can gracefully stop and restart.

As for the initial question, the book covers threading a little bit but does not go further than presenting the EDT, the invokeLater() etc. methods and the SwingWorker.
 
Chet Haase
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SwingWorker is definitely worth checking out; it makes it much easier to spawn worker threads for non-UI tasks and to update the UI (on the proper Swing thread) accordingly.

Also, the in-development "Swing Application Framework" https://appframework.dev.java.net/ adds more capabilities through its "Tasks".

As Ulf pointed out, this is not a problem with Swing, but rather a reality of most/all UI toolkits. Every tried to execute a long-running task inside the button-clicked event in a win32 application? You'll get the same app-frozen effect. The trick is to figure out how to take advantage of Java's threading model to make sure that your UI is snappy (handling only UI-specific tasks) while the non-UI logic happens elsewhere.

Chet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!