What I consider to be a utility class โ usually a bag with undefined public static methods โ rarely requires any kind of synchronization. If a class does not support some kind of mutable state, you are usually absolutely fine.
Of course, if you take parameters that themselves are shared between threads and contain mutable state, you may need synchronization - but this should be for the calling user, usually.
If you mean something else in the "utility class", it would be nice to know what you mean. If it is a class without a mutable state (but possibly an immutable state set during construction), then it is usually perfectly divided between threads.
If it has a mutable state but is not explicitly associated with threads, I would usually not put any synchronization in this class, but document that it is not thread safe. Typically, callers need to synchronize multiple operations using multiple objects, so synchronizing the โmethod at a timeโ usually does not help.
If this is a class that is related to threading (for example, something to control the producer / consumer queues), I would try to make it thread safe, but document what you mean by that. I would advise you not to synchronize the methods themselves, but instead to synchronize in a closed final field, which is used only for synchronization; this way, your class will contain the only code that can be synchronized on this object, which simplifies the discussion about your lock.
You will almost certainly not make these decisions based on performance. Correctness and simplicity in most cases are much more important than performance.
Jon skeet
source share