Feb 8th, 2012, 05:04 PM
Do I have to worry about thread safety of the methods of singletons in the container?
I'm planning on wiring up services and controllers in my container. Do I need to make all of the service methods synchronized since all controller threads will be using a single instance of each service?
I realize I could configure the container to deliver a new instance each time. What I'm confused about is what is the developers responsibility for singletons, i.e. do they need to have synchronized methods in order to be thread safe.
Feb 8th, 2012, 05:18 PM
It depends on the service and the implementation of the service methods. If the class has no instance data, then you're pretty safe. If the only instance data is only set at bean initialization time, then you're still pretty safe. Past that, you have to examine the usage scenarios to see if you need to synchronize any critical sections to prevent data corruption.