问题
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 withdone(), but I think that there no guarantee that implemented methods fromFutureends correctly, still there this Bugno idea about your code about lock/unlock another thread or process, but I suggest to use
Executorfor multithreading,how to get exceptions from
SwingWorkerTaskpersonally I never ever had bad experiences with
SwingWorkeror some un-expected lack, but allMultiThreadingGurus told aboutSwingWorkerfor Production code never, there is still required use ofRunnable#Threadinstead ofSwingWorker
来源:https://stackoverflow.com/questions/7678841/is-a-swingworker-guaranteed-to-throw-a-state-property-change-event-on-completion