Swing Is Non Thread-Safe Inwards Coffee - What Does It Mean? Resultant Dispatcher, Swingworker, Multithreading In Addition To Best Practices

Couple of my reader inquire question, what does it hateful past times Swing is non thread-safe too How does it acquit upon coding inwards Swing GUI application? This postal service is an endeavour to assistance those readers too several other programmers to empathise Swing too thread-safety inwards a fleck to a greater extent than detailed way. To leave of absence on it simple, let's revise what does it hateful past times existence thread-safe? We say an object is thread-safe, if nosotros tin strength out telephone telephone it's method, which tin strength out alter it's state, from multiple thread at same time. To give yous an example, java.lang.String is a thread-safe class, which way yous tin strength out telephone telephone 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 upwards dorsum to Swing now, Core purpose of Swing is made upwards of dissimilar GUI element e.g. JLable, JPanel, JCombobox etc. All these components are not thread-safe, which way yous tin strength out non telephone telephone 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 strength out 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 produce yous update components from other thread? too which methods of Swing API are thread-safe? etc. We volition run into answer of this interrogation inwards side past times side section.



How to update Swing Component from dissimilar thread

blocking method too blocks until GUI is updated, land invokeLater() is an asynchronous telephone telephone too doesn't aspect for GUI to live on 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 larn to a greater extent than nearly both of these methods too How to exercise them, See when to exercise InvokeAndWait too InvokeLater inwards Java.

Couple of times, Interviewer equally good inquire about, how produce 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 strength out live on used to notice 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 upwards operator overloading

It's the determination taken past times at that topographic point designer, at that time. Since making an API thread-safe takes a lot of work, too it's frequently based upon produce goodness yous get. Since GUI screens are by too large 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 rattling rare, when an update asking for GUI comes from a dissimilar thread e.g. may live on 1 time a network asking is consummate or a file is loaded. 

In such cases, yous tin strength out exercise either invokeAndWait(), invokeLater() or preferably SwingWorker class, which is designed to run these lengthy fourth dimension consuming chore inwards a carve upwards 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 outcome too updating GUI, yous must non produce whatever fourth dimension consuming chore on it, otherwise, yous hazard your application to leave of absence unresponsive or frozen. Another cardinal affair to note, land using multi-threading inwards Swing evolution is that, non alone GUI components but at that topographic point model e.g. TableModel for JTable, must live on 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 equally good a listing of Java Swing best practices inwards multithreading environment. When yous exercise threads inwards Swing development, yous hazard of deadlock too frozen GUI. By next these elementary rules, yous minimize your chances of errors.

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

2) Not alone Swing components but equally good at that topographic point model e.g. ListModel, TableModel must live on modified from Event Dispatcher thread.

3) AWT Event Dispatcher thread is responsible for updating GUI too listening events, then don't block them past times doing fourth dimension consuming chore there.

4) You tin strength out exercise java.awt.EventQueue's isDispatchThread() or SwingUtilities.isEventDispatchThread() to cheque if electrical flow thread is EDT.

5) Use SwingWorker to perform lengthy fourth dimension consuming chore 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 past times Swing existence non thread-safe. We receive got equally good 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 interrogation from threading.

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

Belum ada Komentar untuk "Swing Is Non Thread-Safe Inwards Coffee - What Does It Mean? Resultant Dispatcher, Swingworker, Multithreading In Addition To Best Practices"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel