swingWorker 是否保证在完成时抛出状态属性更改事件?

发布于 2024-12-08 19:23:11 字数 680 浏览 2 评论 0原文

我有一个类,它接受许多 SwingWorkers 并按一定顺序一次运行它们。我使用 ReentrantLock 来确保一次只有一个工作线程运行。通常我总是会在finally子句中解锁,但我需要它保持锁定状态直到工作人员完成。

        nextWorker.getPropertyChangeSupport().addPropertyChangeListener("state", 
        new PropertyChangeListener()
        {
           @Override
           public void propertyChange(PropertyChangeEvent evt)
           {
              if (evt.getNewValue().equals(SwingWorker.StateValue.DONE))
              {
                 executionLock.unlock();
              }
           }
        });

如果不是这种情况,是否保证调用done()?我不想以这种方式调用解锁,因为它会在某种程度上违反封装。

由于项目的性质,这很可能会在代码审查中出现。在这种情况下,拥有可验证的来源会很有帮助。到目前为止我还没有找到一个。

I have a class that takes in a number of SwingWorkers and runs them one at a time in a certain order. I use a ReentrantLock to make sure that only one worker runs at a time. Normally I would always unlock in a finally clause, but I need it to stay locked until the worker completes.

        nextWorker.getPropertyChangeSupport().addPropertyChangeListener("state", 
        new PropertyChangeListener()
        {
           @Override
           public void propertyChange(PropertyChangeEvent evt)
           {
              if (evt.getNewValue().equals(SwingWorker.StateValue.DONE))
              {
                 executionLock.unlock();
              }
           }
        });

If this is not the case, is done() guaranteed to be called? I would prefer not to call unlock this way, as it would violate encapsulation to some degree.

Due to the nature of the project it is likely that this will come up in a code review. In that case it would be helpful to have a verifiable source. So far I have been unable to find one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

杀手六號 2024-12-15 19:23:11
  • 就我个人而言,我尝试了 SwingWorker 的一切可能,但总是以 done() 结束,但我认为不能保证实现来自 Future< 的方法/code> 正确结束,仍然有这个 错误


  • 不知道有关锁定/解锁另一个的代码线程或进程,但我建议使用 Executor多线程,

  • 如何获取 来自 SwingWorker 任务的异常

  • 就我个人而言,我从来没有在使用 SwingWorker 时遇到过不好的经历,也没有遇到过一些意外的缺乏,但所有 MultiThreading 大师都如此从来没有告诉过用于生产代码的 SwingWorker,仍然需要使用 Runnable#Thread 而不是 SwingWorker

  • personally I tried everything possible with SwingWorker, but always ends me with done(), but I think that there no guarantee that implemented methods from Future ends correctly, still there this Bug

  • no idea about your code about lock/unlock another thread or process, but I suggest to use Executor for multithreading,

  • how to get exceptions from SwingWorker Task

  • personally I never ever had bad experiences with SwingWorker or some un-expected lack, but all MultiThreading Gurus told about SwingWorker for Production code never, there is still required use of Runnable#Thread instead of SwingWorker

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