#What is the Event Dispatch Thread?

1 messages · Page 1 of 1 (latest)

storm jasper
#

I need a detailed understanding of the event dispatch thread to properly understand why they put SwingUtilities.invokeLater(new Runnable() {
public void run() {
etc............
}
});

worn steepleBOT
#

<@&987246487241105418> please have a look, thanks.

frozen jacinth
#

which part of it is unclear to u?

storm jasper
#

oh thank you:)

#

Mainly the invokeLater method.

#

I have basic knowledge of threads and I know Swing is thread unsafe

frozen jacinth
#

as the name implies, it runs the given runnable later

storm jasper
#

What do you mean by later

river plazaBOT
#
public static void invokeLater(Runnable doRun)

Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.

Runnable doHelloWorld = new Runnable() {
    public void run() {
        System.out.println("Hello World on " + Thread.currentThread());
    }
};

SwingUtilities.invokeLater(doHelloWorld);
System.out.println("This might well be displayed before the other message.");

If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread).

Additional documentation and examples for this method can be found in Concurrency in Swing.

As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater().

Unlike the rest of Swing, this method can be invoked from any thread.

frozen jacinth
#

and not before

storm jasper
frozen jacinth
#

once swing is fully setup and the ui is build up and all

#

imagine ur code would trigger a button click but the button hasnt spawned yet and the click wont be executed

#

u prevent issues like that by waiting with ur code until swing is ready

storm jasper
#

So the classes are loading?

frozen jacinth
#

no, until the ui is ready

#

and the lifecycles are up and running

storm jasper
#

what do you mean when ui is built up?

frozen jacinth
#

imagine u start discord. is it up and ready within 5 nanoseconds? no, it takes a few seconds

storm jasper
#

so it makes sure the button is loaded before it is visible and interactable

#

or any component

frozen jacinth
#

it makes sure that the code u put there in the runnable is executed AFTER swing and ur UI is ready to be interacted with by ur code

storm jasper
#

I think asking this question will help me understand better: why aren't you creating a thread object instead of overriding run in an anonymous class .

#

You have to put the runnable as an argument for a thread object right? Why arent you doing it here

frozen jacinth
#

bc this has nothing to do with creating threads at all

storm jasper
#

so swing is running on the edt no matter what

frozen jacinth
#

that method wants to know what code u want to run. so it asks for a Runnable

#

just bc threads ask for runnables doesnt means others can't ask for runnables as well

#

🙂

storm jasper
#

What could happen if you didn't include SwingUtilities.invokeLater(new Runnable {})

frozen jacinth
#

if that code u want to run there attempts to interact with some of the swing ui it could potentially not work

#

as the system isnt ready yet

storm jasper
#

Oh. So you may make a swing component beforehand and attempt to interact with the component but it hasn't been created yet. It is kind of like .join().... but that doesn't sound right though

frozen jacinth
#

it will put ur code into the event queue, at the end. so its executed once everything else in thr event queue (in particular the startup) is done first

#

so u can have a controlled execution without stuff interferring with each other

storm jasper
#

what would be the startup-

frozen jacinth
#

i feel like we are turning in circles

storm jasper
#

Yes

#

My knowledge is obviously not there'

frozen jacinth
#

maybe u should step back and do the prerequisites first

storm jasper
#

and I have been studying java for almost a year now

frozen jacinth
#

either case, im out for today, cheerio

storm jasper
#

it actually makes sense now. thank you

jolly bobcat
#

@storm jasper the purpose of this method is to run on swing thread something that wasn't on swing thread