I recently saw a MVC java application in which the main method was written as:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
View view = new View();
Model model = new Model();
Controller controller = new Controller(view, model);
controller.start();
}
});
}
Wouldn't this make all the program (including both the model and the controller, that have nothing to do with Swing at all) run until the code ends in the AWT Event Dispatch Thread instead of the Main thread?
If this last was true, then that would be really bad for the app as it would block the EDT from carrying out the tasks it needs to (dispatching events, for example, as the model could be calculating other tasks). Is it correct?
There is a similar old post (not a duplicate from this one) that can suggest the code mentioned above is good practice, so it confused me even more.
invokeLater(...)
as well, or use a SwingWorker instead of a Thread. – Vaultrun()
method starts (and therefore no events to process, no repainting to be done). It stands to reason that whatcontroller.start()
does is make the whole UI visible and before that there is just nothing else to be done on the EDT. – HorologistinvokeLater(...)
only in the view methods that really need to create/update the Swing UI? I don't see why should they all run in the EDT instead of the main thread. – Aledarun()
methods ends. While you could split these tasks between main thread and EDT (and possibly gain a few milliseconds until the UI is first shown) it would also complicate the design of the application (multithreading is no easy topic) and litter the code base withinvokeLater()
calls. I would not do it until someone proves it to be necessary. – HorologistinvokeLater()
on the main. But what if when creating the model it needs to read some files and instead of delaying a few milliseconds it delays a whole second? The point is that every code is different, and I believe only code creating or updating Swing should go to the EDT. With the approach you suggest, the EDT becomes the main thread. – Aleda