#What is the Event Dispatch Thread?
1 messages · Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
which part of it is unclear to u?
oh thank you:)
Mainly the invokeLater method.
I have basic knowledge of threads and I know Swing is thread unsafe
as the name implies, it runs the given runnable later
What do you mean by later
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.
in simple terms, it makes sure the code is executed once ur swing is ready for it
and not before
It makes sure your swing code is running in one thread - but when is the swing ready for it -
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
So the classes are loading?
what do you mean when ui is built up?
imagine u start discord. is it up and ready within 5 nanoseconds? no, it takes a few seconds
so it makes sure the button is loaded before it is visible and interactable
or any component
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
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
bc this has nothing to do with creating threads at all
so swing is running on the edt no matter what
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
🙂
What could happen if you didn't include SwingUtilities.invokeLater(new Runnable {})
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
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
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
what would be the startup-
i feel like we are turning in circles
maybe u should step back and do the prerequisites first
and I have been studying java for almost a year now
either case, im out for today, cheerio
it actually makes sense now. thank you
@storm jasper the purpose of this method is to run on swing thread something that wasn't on swing thread