swingWorker 是否保证在完成时抛出状态属性更改事件?
我有一个类,它接受许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我尝试了
SwingWorker
的一切可能,但总是以done()
结束,但我认为不能保证实现来自Future< 的方法/code> 正确结束,仍然有这个 错误
不知道有关锁定/解锁另一个的代码线程或进程,但我建议使用
Executor
多线程,如何获取 来自
SwingWorker
任务的异常就我个人而言,我从来没有在使用
SwingWorker
时遇到过不好的经历,也没有遇到过一些意外的缺乏,但所有MultiThreading
大师都如此从来没有告诉过用于生产代码的SwingWorker
,仍然需要使用Runnable#Thread
而不是SwingWorker
personally I tried everything possible with
SwingWorker
, but always ends me withdone()
, but I think that there no guarantee that implemented methods fromFuture
ends correctly, still there this Bugno 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
Taskpersonally I never ever had bad experiences with
SwingWorker
or some un-expected lack, but allMultiThreading
Gurus told aboutSwingWorker
for Production code never, there is still required use ofRunnable#Thread
instead ofSwingWorker