java publish_java – 如何正确使用SwingWorker上的publish()和process()方法?

SwingWorker中的这两种方法让我感到困惑,似乎很容易错误地使用它们.

方法publish()描述了以下内容:

Sends data chunks to the process(java.util.List) method. This method is to be used from inside the doInBackground method to deliver intermediate results for processing on the Event Dispatch Thread inside the process method.

这对我来说意味着当我的工作线程正在执行其doInBackground()方法时,我能够创建“块”(这些是特定的或者这只是一种引用消息对象的方式吗?),然后发布它们进行处理在我的Swing GUI上.

这导致我process().javadoc概述了以下内容:

Receives data chunks from the publish method asynchronously on the Event Dispatch Thread.

在查看了这两种方法的文档后,是否有人可以澄清这种方法背后的机制是什么?据我所知,这是一个异步过程,但由于它是在EDT上进行的,我想象中有一些可预测性.

publish()文档说明了这一点:

Because the process method is invoked asynchronously on the Event Dispatch Thread multiple invocations to the publish method might occur before the process method is executed. For performance purposes all these invocations are coalesced into one invocation with concatenated arguments.

总结一下,我的问题有两个:

>谁应该调用进程()?

>在SwingWorker和EDT的上下文中,process()的工作流程是什么?

如果需要澄清,请告诉我.

解决方法:

我编写了一个程序来处理子树中的目录.处理过程需要足够长的时间,以至于用户需要一些正在进行的反馈.我建立了一个对话框,其中包含一个标签,其中包含当前正在处理的子目录的名称; swingworker线程调用publish(),其中包含正在处理的每个目录的名称.

无法保证为每个目录名更新UI – 有时处理速度比重新显示更快,特别是如果计算机上发生了其他任何事情,并且取决于运行的计算机.因此,可以调用publish()并执行多次,而不会由Swing运行时调用process().

正如已经指出的那样,代码不会调用进程 – 在调用publish之后代表您调用,也许并非每次调用publish()时调用.

这会为你清理吗?

标签:java,swing,swingworker

来源: https://codeday.me/bug/20190517/1121597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值