Why are locks Serializable in java?

One question arose in my mind while studying the implementation of the ReentrantLock class. ReentrantLock is serialized, and the documentation says that any deserialized lock always unlocks regardless of the state when it was serialized. This makes sense because locking and unlocking the state is mainly based on threads at runtime (which hold the lock), and while we deserialize these threads, they may not be available.

Question: why should we block so that it is saved, because it does not save its main state (locked / unlocked)? Now I can assume that this may be for the property of justice of the castle. But justice again depends on the underlying OS, so if we save the lock on one platform and deserialize the other, because (write once and run anywhere), it may not work, so do not persist in justice only.

Hopefully I clearly put my confusion on locking serialization in java.

+6
source share
1 answer

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html http://download.java.net/jdk7/archive/b123/docs/api/java /util/concurrent/locks/ReentrantLock.html

I would say that the reason the lock is saved is because you can serialize objects that depend on the lock itself. If the lock was not serialized, all those dependent on it could also not be serialized.

You can also save the owner, holdCount, queuedThreads and all the other materials that you can see on the API page that I linked above for ReentrantLock.

+5
source

Source: https://habr.com/ru/post/950752/