I am trying to make a Java application thread-safe. Unfortunately, it was originally designed for a single-user mode and all the key classes are instantiated as singletons. To make matters worse, there is a whole bunch of interfaces working as constants containers and numerous static fields.
What would be considered as a good practice in this case?
- There is a single entry point, so I could synchronize that and just use pooling (sort of), but if the calls take more than a minute on average, all other threads in the queue would have to wait for a long time…
- Since the test code coverage is not quite optimal and I can’t be sure if I overlooked something, some hints about bad implementation patterns (similar to those stated above) in this area would be useful.
- I know the best thing would be to rewrite the whole structure, but this isn’t an option.
@coldphusion, you’ll have to read/analyze code. Using an automated tool, if such a tool exists, would be like shooting yourself in the foot.
Plus, not everything has to be thread-safe. If an object will never be accessed from multiple threads, no need to make it thread-safe. If an object is immutable, then it’s already thread-safe.
Be ready to tell your boss “It won’t take a few hours or a day, even you know it, so stop asking.”
I recommend reading Java Concurrency In Practice.