invokeLater vs invokeAndWait with JTextArea

痞子三分冷 提交于 2019-12-08 14:10:37

问题


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


回答1:


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, the display() parameter s is a final reference to an immutable String; it can be accessed safely in display() without further synchronization. If you have a final reference to a mutable object, consider a thread-safe collection. You can look for violations using one of the approaches cited here. Alternatively, consider a SwingWorker to host the background task, for example.



来源:https://stackoverflow.com/questions/33708881/invokelater-vs-invokeandwait-with-jtextarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!