Java

Java

Made by DeepSource

readObject should not be synchronized JAVA-W1093

Anti-pattern
Major
Autofix

Marking the readObject method as synchronized is useless, as this method will never be used with an object that is shared across threads.

Using the synchronized modifier is thus not required and may actually be confusing.

readObject is implemented to specify custom deserialization logic for the JVM to use. It is never called in a multithreaded context by the JVM itself.

Additionally, this method is never meant to be called explicitly by non-JVM code, which means there is never a need to use it with synchronization.

Thus, it is not recommended to add the synchronized modifier to readObject.

Bad Practice

private synchronized void readObject(ObjectInputStream oiStream)
        throws IOException, ClassNotFoundException {
    // ...
}

Recommended

Remove the synchronized modifier.

private void readObject(ObjectInputStream oiStream)
        throws IOException, ClassNotFoundException {
    // ...
}