My extended SwingWorker class performs a potentially reoccurring background task which requires GUI originating input variables.
I see 2 coding options:
-
To start a new instance of the class each time I use it and pass
the variables to the constructor. I presume I should make sure there
are not to many instances. If so how? multiton or some other method? -
Update the variables and call execute again? If so how do I make
sure i’m not interrupting?
Is one of these options the way to go or is there a better way?
Plase use option 1.
Immutable objects are normally easier to work with. For example, you prevent the problem that the variables are updated while the worker is still working, and you have to think less about memory visibility.
Object instantiation is quite cheap in Java, so this won’t be a performance problem and you can create a new instance every time to need one.