I have worker threads that generate objects and push them into a thread-safe Set
. A processing thread periodically reads the Set
and processes the elements.
While the object references themselves will be successfully retrieved from the set, the objects' variables are not thread-safe if accessed from the processing thread. Is there some pattern to do this, apart from making all the objects' internals volatile
etc.? The objects may become more complex in the future, containing nested objects etc.
Assuming that no object will be externally modified by once placed into the Set
, is there some way to "happens-before" whatever is currently in the Set
before I begin processing it? The processing thread is already running and will not be created only after the Set
has been populated.
The objects themselves are just data containers and have no inherent thread-safety. I can't make all the fields final since they may be modified multiple times before being placed into the Set
.