Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use a separate thread to show a GUI in JAVA

This simple issue confuses me. You can display a JAVA GUI application by setting the frames' setVisible property true. But in almost all the examples I found on internet they use a separate thread to do the same thing.

They do something like this,

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        new Frame().setvisible(true);  //just take the idea of this line
    }
});

I found no difference between the two methods. But there must be some special reason, that's why everyone is doing this.

Can someone explain it..thanks!

like image 468
Anubis Avatar asked Oct 01 '12 03:10

Anubis


People also ask

Should main thread have GUI?

GUI Thread and Worker Thread As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads.

Why are swings not thread safe?

Most Swing object methods are not "thread safe". This means that if those (non thread safe) method are invoked from multiple threads this could result in thread interference or memory consistency errors. Only thread safe methods can be safely invoked from any thread.

What is the purpose of GUI in Java?

GUI (Graphical User Interface) in Java is an easy-to-use visual experience builder for Java applications. It is mainly made of graphical components like buttons, labels, windows, etc. through which the user can interact with an application. GUI plays an important role to build easy interfaces for Java applications.

Which Swing method is thread safe?

Many important methods of Swing components are explicitly documented as threadsafe. These include the JComponent repaint() and revalidate() methods, many methods of Swing text components, and all listener add and remove methods.


2 Answers

The main reason for launching your application in this way is that Swing components are not thread-safe so you need to guarantee which thread your GUI will start from: the one called the Event Dispatching Thread (EDT). Without doing this, you can't be sure what thread it will start in, but as noted by several kind commentators, the main thread is guaranteed not to be the EDT.

You should only create, access, or modify UI components from within the EDT. Doing otherwise will result in unexpected behavior (if you're lucky) and/or dirty repaints.

Some resources I suggest you become familiar with:

  • The Event Dispatch Thread
  • Painting in AWT and Swing

You could also have a read of Why does my boilerplate Java desktop app JFrame use EventQueue.invokeLater in the main method?

UPDATE

This is the blog I've been trying to find :P

This basically explains why it's important to sync your main with the EDT before getting started, it also describes some of the details about why.

It also describes why many developers make this fundamental mistake when starting their applications (basically, we were told we could, but we never were really allowed to...bad us)

like image 159
MadProgrammer Avatar answered Oct 10 '22 02:10

MadProgrammer


Because every modification you do on the GUI should be done on the event dispatching thread. This is how AWT and SWING are meant to work.

This because the redraw is executed on a single thread, by using invokeLater you let that thread manage it without having potential issued by the lack of thread safety of Swing. Using that syntax you delegate that instructions to be executed on the appopriate thread, which is the one that manages the GUI elements.

like image 36
Jack Avatar answered Oct 10 '22 03:10

Jack