Swing Is Non Thread-Safe Inward Coffee - What Does It Mean? Final Result Dispatcher, Swingworker, Multithreading Together With Best Practices

Couple of my reader inquire question, what does it hateful past times Swing is non thread-safe as well as How does it touching coding inwards Swing GUI application? This post is an examine to assistance those readers as well as several other programmers to empathise Swing as well as thread-safety inwards a fleck to a greater extent than detailed way. To locomote along it simple, let's revise what does it hateful past times beingness thread-safe? We say an object is thread-safe, if nosotros tin telephone hollo upward it's method, which tin alter it's state, from multiple thread at same time. To give yous an example, java.lang.String is a thread-safe class, which agency yous tin telephone hollo upward whatever method e.g. substring(), toUpperCase() or toLowerCase() from multiple threads. By the way, String is thread-safe because it's immutable. Let's come upward dorsum to Swing now, Core business office of Swing is made upward of unlike GUI ingredient e.g. JLable, JPanel, JCombobox etc. All these components are not thread-safe, which agency yous tin non telephone hollo upward methods of this components e.g. JLable.setText("new title") from whatever thread, other than Event Dispatcher Thread(EDT)

In 1 word, Since Swing is non thread-safe, yous tin non update Swing components from whatever random thread, they are e'er updated using Event Dispatcher thread. This is inwards fact 1 of the most popular Java Swing Interview Question, amongst lot's of interesting follow-up e.g. 

If Swing is non thread-safe than how create yous update components from other thread? as well as which methods of Swing API are thread-safe? etc. We volition come across respond of this interrogation inwards side past times side section.



How to update Swing Component from unlike thread

blocking method as well as blocks until GUI is updated, spell invokeLater() is an asynchronous telephone hollo upward as well as doesn't human face for GUI to last updated. By the way, both of these method brand certain that Swing GUI components are updated inwards EDT thread only. Both of these method takes a Runnable object, which contains code to update GUI, every bit shown below :

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    JLable.setText("Update Title");
  }
}

to acquire to a greater extent than nearly both of these methods as well as How to operate them, See when to operate InvokeAndWait as well as InvokeLater inwards Java.

Couple of times, Interviewer besides inquire about, how create yous find, if a especial thread is Event Dispatcher thread or not? Well, if yous are familiar amongst Swing API, they yous may know that SwingUtilities provides isEventDispatchThread() method, which tin last used to respect out if electrical flow thread is Event Dispatcher thread or not. Another follow-up interrogation is Why Swing is non thread-safe inwards Java? Well, this is on similar lines similar why multiple inheritance is non supported inwards Java or Why Java doesn’t back upward operator overloading

It's the determination taken past times at that spot designer, at that time. Since making an API thread-safe takes a lot of work, as well as it's frequently based upon create goodness yous get. Since GUI screens are generally updated inwards reply of user activeness e.g. when user click a button, as well as since events are handled inwards the same Event dispatcher thread, it's slow to update GUI on that thread. It's rattling rare, when an update asking for GUI comes from a unlike thread e.g. may last in 1 lawsuit a network asking is consummate or a file is loaded. 

In such cases, yous tin operate either invokeAndWait(), invokeLater() or preferably SwingWorker class, which is designed to run these lengthy fourth dimension consuming draw of piece of occupation inwards a split thread as well as same fourth dimension managing inter thread communication betwixt worker thread as well as Event dispatcher thread. 

Since EDT thread is most of import inwards Swing as well as responsible for listening effect as well as updating GUI, yous must non create whatever fourth dimension consuming draw of piece of occupation on it, otherwise, yous peril your application to locomote unresponsive or frozen. Another cardinal matter to note, spell using multi-threading inwards Swing evolution is that, non solely GUI components but at that spot model e.g. TableModel for JTable, must last updated inwards Event Dispatcher thread. One of the crucial difference betwixt AWT as well as Swing is that AWT components are thread-safe

Summary

Here is summary of our discussion, which is besides a listing of Java Swing best practices inwards multithreading environment. When yous operate threads inwards Swing development, yous peril of deadlock as well as frozen GUI. By next these uncomplicated rules, yous minimize your chances of errors.

1) Since Swing components are non thread-safe, until whatever specifics mentioned inwards Javadoc, they must last created as well as modified from AWT Event Dispatcher thread.

2) Not solely Swing components but besides at that spot model e.g. ListModel, TableModel must last modified from Event Dispatcher thread.

3) AWT Event Dispatcher thread is responsible for updating GUI as well as listening events, as well as therefore don't block them past times doing fourth dimension consuming draw of piece of occupation there.

4) You tin operate java.awt.EventQueue's isDispatchThread() or SwingUtilities.isEventDispatchThread() to banking enterprise friction match if electrical flow thread is EDT.

5) Use SwingWorker to perform lengthy fourth dimension consuming draw of piece of occupation inwards worker thread.
6) Use InvokeAndWait() as well as invokeLater() to update GUI components from threads other than Event Dispatcher thread.

That's all on Why Swing is non thread-safe inwards Java, to a greater extent than of import what does it hateful past times Swing beingness non thread-safe. We receive got besides touched base of operations on Event Dispatcher thread, SwingWorker, invokeAndWait, as well as invokeLater. This is an extremely of import topic from Swing evolution as well as Interview indicate of view. I receive got never seen a Java Swing Interview, without whatever interrogation from threading.

Further Reading
Java Swing (GUI) Programming: From Beginner to Expert
Mastering Java Swing - Part 1
The Complete Java MasterClass


Sumber https://javarevisited.blogspot.com/

0 Response to "Swing Is Non Thread-Safe Inward Coffee - What Does It Mean? Final Result Dispatcher, Swingworker, Multithreading Together With Best Practices"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel