The question is very general, so please do not expect any specifics from my answer.
1_ We need to be more careful about static methods than using instance methods.
2_ @Justmycorrectopinion is correct, but some of the terms he described need to be more developed to be perfect. (Even if the static method only works with a local variable, there is still a chance of a race condition.)
3_ There are simple rules for me that have helped me analyze thread safety.
Understand if all the components encapsulated inside it are common or not. Thus, the simplest solution is to reduce the volume of the entire variable and increase the volume, if absolutely necessary, and if the component performs a mutation on the object, it is usually not thread safe.
4_ Use instrumental support to perform static thread safety code analysis. (The idea has a checkthread plugin).
5_ Never use a static method to mutate an object. If calling a static variable causes a mutation of the object, the developer simply bypasses OOPS.
6_ Always document thread safety. Remember that some method may not need to be synchronized during development, but it can be easily done without streaming.
7_ Last, but probably most important, make sure most of your objects are immutable. In my experience, most of the time I never had to change many of my objects. (In rare cases when it is necessary to change the state of an object, protective copying / creating a new object is almost always better.)
uncaught_exceptions
source share