SwingWorker 得到结果

发布于 2024-12-22 21:31:44 字数 123 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-12-29 21:31:44

覆盖 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 6

Also 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 call get from the Event Dispatch Thread (EDT) your GUI will be unresponsive if the worker isn't done. You can call isDone 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.

呆头 2024-12-29 21:31:44

您可以重写 done() 和/或 process(),如此处所示此处

You can override done() and/or process(), as shown here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文