Swing Is Non Thread-Safe Inwards Coffee - What Does It Mean? Outcome Dispatcher, Swingworker, Multithreading As Well As Best Practices

Couple of my reader enquire question, what does it hateful yesteryear Swing is non thread-safe too How does it deport upon coding inwards Swing GUI application? This post service is an assay to aid those readers too several other programmers to empathize Swing too thread-safety inwards a chip to a greater extent than detailed way. To boot the bucket along it simple, let's revise what does it hateful yesteryear existence thread-safe? We say an object is thread-safe, if nosotros tin telephone outcry upwardly it's method, which tin alter it's state, from multiple thread at same time. To give you lot an example, java.lang.String is a thread-safe class, which agency you lot tin telephone outcry upwardly 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 upwardly dorsum to Swing now, Core usage of Swing is made upwardly of dissimilar GUI constituent e.g. JLable, JPanel, JCombobox etc. All these components are not thread-safe, which agency you lot tin non telephone outcry upwardly methods of this components e.g. JLable.setText("new title") from whatever thread, other than Event Dispatcher Thread(EDT)

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

If Swing is non thread-safe than how produce you lot update components from other thread? too which methods of Swing API are thread-safe? etc. We volition run into response of this query inwards side yesteryear side section.



How to update Swing Component from dissimilar thread

blocking method too blocks until GUI is updated, piece invokeLater() is an asynchronous telephone outcry upwardly too doesn't expression for GUI to hold upwardly 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, equally shown below :

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

to acquire to a greater extent than almost both of these methods too How to usage them, See when to usage InvokeAndWait too InvokeLater inwards Java.

Couple of times, Interviewer likewise enquire about, how produce you lot find, if a exceptional thread is Event Dispatcher thread or not? Well, if you lot are familiar alongside Swing API, they you lot may know that SwingUtilities provides isEventDispatchThread() method, which tin hold upwardly used to discovery out if electrical current thread is Event Dispatcher thread or not. Another follow-up query 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 upwardly operator overloading

It's the conclusion taken yesteryear at that spot designer, at that time. Since making an API thread-safe takes a lot of work, too it's oftentimes based upon produce goodness you lot get. Since GUI screens are generally updated inwards response of user activity e.g. when user click a button, too since events are handled inwards the same Event dispatcher thread, it's slow to update GUI on that thread. It's really rare, when an update asking for GUI comes from a dissimilar thread e.g. may hold upwardly in i trial a network asking is consummate or a file is loaded. 

In such cases, you lot tin usage either invokeAndWait(), invokeLater() or preferably SwingWorker class, which is designed to run these lengthy fourth dimension consuming business inwards a carve upwardly thread too same fourth dimension managing inter thread communication betwixt worker thread too Event dispatcher thread. 

Since EDT thread is most of import inwards Swing too responsible for listening number too updating GUI, you lot must non produce whatever fourth dimension consuming business on it, otherwise, you lot peril your application to boot the bucket unresponsive or frozen. Another telephone commutation affair to note, piece using multi-threading inwards Swing evolution is that, non exclusively GUI components but at that spot model e.g. TableModel for JTable, must hold upwardly updated inwards Event Dispatcher thread. One of the crucial difference betwixt AWT too Swing is that AWT components are thread-safe

Summary

Here is summary of our discussion, which is likewise a listing of Java Swing best practices inwards multithreading environment. When you lot usage threads inwards Swing development, you lot peril of deadlock too frozen GUI. By next these elementary rules, you lot minimize your chances of errors.

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

2) Not exclusively Swing components but likewise at that spot model e.g. ListModel, TableModel must hold upwardly modified from Event Dispatcher thread.

3) AWT Event Dispatcher thread is responsible for updating GUI too listening events, too hence don't block them yesteryear doing fourth dimension consuming business there.

4) You tin usage java.awt.EventQueue's isDispatchThread() or SwingUtilities.isEventDispatchThread() to banking venture lucifer if electrical current thread is EDT.

5) Use SwingWorker to perform lengthy fourth dimension consuming business inwards worker thread.
6) Use InvokeAndWait() too 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 yesteryear Swing existence non thread-safe. We receive got likewise touched base of operations on Event Dispatcher thread, SwingWorker, invokeAndWait, too invokeLater. This is an extremely of import topic from Swing evolution too Interview indicate of view. I receive got never seen a Java Swing Interview, without whatever query from threading.

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

Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update