Is a swingWorker guaranteed to throw a state prope

2020-04-07 18:21发布

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条回答
家丑人穷心不美
2楼-- · 2020-04-07 18:56
  • 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

查看更多
登录 后发表回答