java SwingWorker.doInBackground() 不得访问 GUI 元素
也许这很微不足道,我正在努力理解 SwingWorker。
下面是复制粘贴的内容
工作流程
SwingWorker 的生命周期涉及三个线程:
当前线程:在此线程上调用execute()方法。它 安排 SwingWorker 在工作线程上执行并返回 立即地。我们可以等待 SwingWorker 完成使用 获取方法。
工作线程:在此线程上调用 doInBackground() 方法。 这是所有后台活动应该发生的地方。通知 关于绑定属性更改的 PropertyChangeListeners 使用 firePropertyChange 和 getPropertyChangeSupport() 方法。默认情况下 有两个可用的绑定属性:状态和进度。
事件调度线程:所有与 Swing 相关的活动都在此发生 线。 SwingWorker 调用 process 和 did() 方法并 通知该线程上的任何 PropertyChangeListener。
通常,当前线程是事件调度线程。
--
工作线程不是 EDT,因此 doInBackground() 中的代码不得访问 GUI 元素。我的理解正确吗?
背景: 我们有一些使用 SwingWorker 的小代码,但有 doInBackground() 创建 FileChooser 并调用 setCurrentDirectory()
。我怀疑这导致我的异常几乎与 https://bugs.java.com/ bugdatabase/view_bug?bug_id=6637181(11-已关闭,不是缺陷)
May be this is trivial, I am struggling to understand a simple documentation on SwingWorker.
Here is the copy pasted content
Workflow
There are three threads involved in the life cycle of a SwingWorker :
Current thread: The execute() method is called on this thread. It
schedules SwingWorker for the execution on a worker thread and returns
immediately. One can wait for the SwingWorker to complete using the
get methods.Worker thread: The doInBackground() method is called on this thread.
This is where all background activities should happen. To notify
PropertyChangeListeners about bound properties changes use the
firePropertyChange and getPropertyChangeSupport() methods. By default
there are two bound properties available: state and progress.Event Dispatch Thread: All Swing related activities occur on this
thread. SwingWorker invokes the process and done() methods and
notifies any PropertyChangeListeners on this thread.Often, the Current thread is the Event Dispatch Thread.
--
The worker thread is not the EDT, hence the code in doInBackground() must not access GUI elements. Is my understanding correct?
Background:
We have small code that uses SwingWorker but has doInBackground() createing FileChooser and calling setCurrentDirectory()
. I suspect that is leading me exception almost same as https://bugs.java.com/bugdatabase/view_bug?bug_id=6637181 ( 11-Closed, Not a Defect)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。从后台线程 - 常规线程和
SwingWorker.doInBackground
中,您不得修改 UI 以避免出现各种问题。相反,将更改包装在
Runnable
中,并通过SwingUtilities.invokeAndWait
、SwingUtilities.invokeLater
或 - 使用SwingWorker
- 通过publish
(来自doInBackground
)。在由 EDT 执行的SwingWorker
的process
方法中,您可以访问 GUI。http://docs.oracle.com/javase/tutorial/uiswing/concurrency /initial.html
就我个人而言,我发现
invokeLater
和invokeAndWait
在许多情况下更容易使用。 SwingWorker 适合用于进度条等。Yes. From a background thread - both regular threads and
SwingWorker.doInBackground
you must not modify the UI to avoid various trouble.Instead, wrap the changes in a
Runnable
and have them executed in the EDT viaSwingUtilities.invokeAndWait
,SwingUtilities.invokeLater
or - when usingSwingWorker
- viapublish
(fromdoInBackground
). Within theprocess
method ofSwingWorker
, which is executed by the EDT, you may access the GUI.http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
Personally, I find
invokeLater
andinvokeAndWait
easier to use for many situations. SwingWorker is okay for e.g. progress bars.