GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Concurrency + Swing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Concurrency + Swing?" Watch "Concurrency + Swing?" New topic
Author

Concurrency + Swing?

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Was curious how many people use Multi-Threading in your swing applications? I was reading up on the Oracle Trail that there are 3 types of Threads that we can use in Swing. We all know and love our Event Dispatch thread, but apparently everything we do in Swing runs on that thread. So it really is the Pre/Post threads that we would have to worry about. Pre thread I would assume would be for anything needing to run before we get to the EDT; however isn't that thread made when we first start coding? I thought I read somewhere that everything starts off in one thread I would assume based on the Main method... So that leaves the post threads? I would assume you could just use the 1 thread we started with(after the EDT is finished) or make a new one...

Just curious what people do?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

For all code that I expect to run longer than a few milliseconds I use SwingWorker (most often), SecondaryLoop (used it once so far) or regular threads (just about never). The alternative is non-responsive user interfaces which is a definite no-no.

Using the "1 thread we started with" (the main thread) doesn't work since it continues and ends when the EDT thread is started. Threads in user interfaces usually are triggered by events by the users (like a button press), so you can't use the main thread unless you want to make it wait for jobs to execute. That makes your code unnecessarily complex though.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Rob Spoor wrote:For all code that I expect to run longer than a few milliseconds I use SwingWorker (most often), SecondaryLoop (used it once so far) or regular threads (just about never). The alternative is non-responsive user interfaces which is a definite no-no.

Using the "1 thread we started with" (the main thread) doesn't work since it continues and ends when the EDT thread is started. Threads in user interfaces usually are triggered by events by the users (like a button press), so you can't use the main thread unless you want to make it wait for jobs to execute. That makes your code unnecessarily complex though.


Yeah that's what it was called, SwingWorker... I'll go check them out.


Ah I see, so does the EDT thread start when you use Swing components, or when is it triggered? Does it stop? I was thinking that a first thread would start and stop when the EDT started, and then when the EDT stopped it would possibly start again... Now I'm thinking that we are always going to be on the EDT.


It makes more sense now to think of it as the ladder because if EDT is always on using the swing components then doing other things in the background you would need to execute another thread since the EDT would be the only one running. Right now for my code I notice that I do calculations and stuff and then do what's needed with the GUI. I guess if you were to do something like a menu item it would run a separate thread and open?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

does the EDT thread start when you use Swing components, or when is it triggered?

Any code wrapped in a SwingUtilities/EventQueue#invokeLater(...)/invokeAndWait(...) will kick off the EDT if it's not already started. Otherwise, the starting of the EDT is an implementation detail that you shouldn't need to worry about. My observations have been that the first construction of any Swing component starts the EDT. Not tested whether constructing an AWT component does the same.

I guess if you were to do something like a menu item it would run a separate thread and open?

Don't guess. Specify. All Swing and AWT event handling code executes on the EDT. If you need a different thread of execution, use one of the three techniques suggested by Rob Spoor.


luck, db
There are no new questions, but there may be new answers.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Darryl Burke wrote:
does the EDT thread start when you use Swing components, or when is it triggered?

Any code wrapped in a SwingUtilities/EventQueue#invokeLater(...)/invokeAndWait(...) will kick off the EDT if it's not already started. Otherwise, the starting of the EDT is an implementation detail that you shouldn't need to worry about. My observations have been that the first construction of any Swing component starts the EDT. Not tested whether constructing an AWT component does the same.

I guess if you were to do something like a menu item it would run a separate thread and open?

Don't guess. Specify. All Swing and AWT event handling code executes on the EDT. If you need a different thread of execution, use one of the three techniques suggested by Rob Spoor.


Ok I wont guess, let me rephrase. I want to know when you would use the other threads, so I made an example of running a menu item and then the menu doing something other than component type changes.... So like I said I'm just curious on what example or things should we look for when trying to do it? Just curious...
 
GeeCON Prague 2014
 
subject: Concurrency + Swing?