Forums Register Login

Concurrency + Swing?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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...
There is no "i" in denial. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1299 times.
Similar Threads
wait() and a freezing gui.
Threads with Swing and deadlock
Help a swing neophyte understand something
Handling threading issues when listening to model events with ModelViewPresenter
Gui refresh
Handling EDT issues when listening to model events with Model View Presenter
Java 1.6.0.20 broke my SwingWorker threads!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:06:50.