I have a Swing application that runs on multiple threads, I created the Swing components on the EDT. An Executor launches threads that insert text to the JTextArea at some point. However, InvokeLater doesn't always do the appending, unlike InvokeAndWait. As I gathered it's aynchronous, non-blocking, but still should do the appending. How can it be?
Thanks
Using
EventQueue.invokeLater()
to update a component's model from another thread is a necessary—but not sufficient—condition for correct synchronization. You are still required to synchronize access to any shared data. In this example, thedisplay()
parameters
is afinal
reference to an immutableString
; it can be accessed safely indisplay()
without further synchronization. If you have afinal
reference to a mutable object, consider a thread-safe collection. You can look for violations using one of the approaches cited here. Alternatively, consider aSwingWorker
to host the background task, for example.