Hi im new in Opp and Java.
I have see something about thread, implements Runnable , start() to call the call the run().
But what does this do?
EventQueue
invokeLater();
Or this full line that you can find in this post on the main method: JTextFields on top of active drawing on JPanel, threading problems
and the Answer on this same on the main method: Java page flipping not supported on Mac OS?
EventQueue.invokeLater(new NewTest());
That call the run() method? what is the difference between that and
Thread var = new Thread(this);
var.start();
The Swing API is single-threaded: you are only allowed to invoke methods on Swing components from the swing thread.
You can have multiple threads in your application, but the moment that another thread needs to interact with the swing components, it needs to do so using EventQueue.invokeLater
. This ensures that the Runnable
is run on the correct thread.
Starting your own Thread
does not have this effect (of running on the Swing thread), so it is not an alternative. If you do it, it may result in data corruption, incorrect screen updates, etc. It may be temporary but it may also be irreparable.
But the invokeLater
method is not an alternative to running your own thread or using a thread pool either since all Runnables
passed to it are executed in sequence, not in parallel.