SwingWorker 得到结果
我有一个 SwingWorker,它在后台执行一些计算(这些操作位于重写的 doInBackground() 方法中)。因此,我还使用 execute()
方法开始计算。当这些计算完成后我怎样才能得到结果?
I have a SwingWorker, which does some computations in the background (these actions are situated in the overridden doInBackground()
method). So, I also use the execute()
method to begin the computations. How I can get the result of these computations when they are finished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
覆盖
done
方法 - 当工作完成时将调用该方法。 Oracle 在这里提供了全面的教程:在 Java SE 6 中使用 SwingWorker 提高应用程序性能另请参阅另一个 SO 问题:如何在 Java 中使用 SwingWorker?
您可以调用
get< /code> 检索结果,但如果工作人员未完成,线程将阻塞,直到工作人员完成为止。这意味着,如果您从事件调度线程 (EDT) 调用
get
,并且工作人员尚未完成,您的 GUI 将无响应。您可以调用isDone
来确定工作线程是否已完成。最后,您可以附加一个属性更改侦听器,以获取工作进程的进度通知,包括它何时完成其任务。我发布的第一个链接给出了一个例子。
Override the
done
method - that method will be called when the work is complete. Oracle has a comprehensive tutorial here: Improve Application Performance With SwingWorker in Java SE 6Also see another SO question: How do I use SwingWorker in Java?
You can call
get
to retrieve the results but if the worker isn't done the thread will block until the worker is done. That means if you callget
from the Event Dispatch Thread (EDT) your GUI will be unresponsive if the worker isn't done. You can callisDone
to determine if the worker has completed.Finally, you can attach a property change listener to be notified of the worker's progress, including when it completes its task. The first link I posted gives an example of that.
您可以重写
done()
和/或process()
,如此处所示和此处。You can override
done()
and/orprocess()
, as shown here and here.