Wether I should create those objects by implementing read/writeObject methods
First off, to have entities that you are serializing manage their own threads is the first red flag. Entities that are serialized should most likely be data passing objects only. Business logic and certainly thread tasks should live elsewhere. Having the readObject()
method magically fork threads just gives me the willies even thinking about it.
I would have the class that is reading these deserialize them and then register them with some sort of entity/task manager. Then the manager would manage the thread pools (think ExecutorService
) that run the tasks the entities need to operate appropriately. Entities, especially serialized ones, should not do that on their own.
run the both factory methods when I want to create them from scratch and use only the transient factory method for the case I read them from a serialized stream?
If you need to precisely manage these objects then you could have the entity manager do the actual construction and task forking I guess. Or just have a register()
method there so whenever someone constructs one or deserializes one of these, they register it.