Is a swingWorker guaranteed to throw a state property change event on completion?

牧云@^-^@ 提交于 2019-12-01 19:41:20

问题


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.


回答1:


  • 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



来源:https://stackoverflow.com/questions/7678841/is-a-swingworker-guaranteed-to-throw-a-state-property-change-event-on-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!