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

Concurrency in SWING  RSS feed

 
Alexandre Baldo
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, guys!

I was reading about Concurrency in Swing at

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html

and I get a little confused.

This tutorial says:

In Swing programs, the initial threads don't have a lot to do. Their most essential job is to create a Runnable object that initializes the GUI and schedule that object for execution on the event dispatch thread.


Like this:


What and didn't understand was that it also say:

Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.


The question is: I think that starting an application is not a simple task. The app will be running in the event dispatch thread until its closed, right?? It will not 'finish quickly'.
So, what's is the correct way to start a gui application? In wich thread? Main thread or event dispatch thread ?

Thank you in advance!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "createAndShowGUI()" method will assemble the frames, panels, buttons, menus, etc, call setVisible(true), and return. It won't keep running forever. Swing GUIs are "event driven", which means that when the user clicks a button (for example), one of the event handler methods you supply gets called on that event thread to react to that click. It's these event handler methods that must return quickly. For example, if clicking a button should run a database query that might take several minutes, then the event handler should start a new thread to run the query and return right away. If the database query ran in the event handler -- i.e., on the event thread -- then the GUI would be frozen the whole time, since no other events could be handled.
 
Alexandre Baldo
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it!

Thank you for your help, Ernest!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!