Why, for example, in java optimistic lock is called that way and not otherwise?

  • The subject of the blocking itself is essentially not related to a specific Japanese. - Dmitriy Simushev

1 answer 1

In the case of using optimistic lock ( optimistic lock ), you first try to perform actions, and only then see if the result does not lead to inconsistency of data (change conflict, etc.). The name itself comes from the fact that with optimistic blocking, the likelihood of a conflict is assumed to be small and you perform actions that are called " good luck " (in the hope that there will be no conflict).

In contrast, when using pessimistic lock ( pessimistic lock ), you completely lock the resource, and only then make actions on it. In this case, you very much doubt that your actions will end without a conflict.

  • If you look at the implementation in Java, for example, then you will not "see if the result leads to data failure." There, optimistic locking is placed on read-methods, while many can read, but not change. And vice versa - when someone calls the read method, other actions can no longer be done. And the threads have to wait. - voipp
  • If a thread has to wait after someone has read the data, then this is a pessimistic lock. Well, then I’m not talking about implementations, but about design patterns: optimistic blocking , pessimistic blocking . - Dmitriy Simushev
  • Well, as you can see, the implementation of this pattern in Java does not match your description. The stream that writes is forced to jat, and which reads is not. Is it pessimistic ?! - voipp
  • Yes, this is a pessimistic lock. And the description is not mine initially, but by Martin Fowler (see links). =) I would rather believe that something is wrong with your implementation (or your interpretation of it) than with terminology on locks. - Dmitriy Simushev