Pessimistic locking and optimistic locking in java

In the recent interview, the interviewer mentioned pessimistic locks and optimistic locks. I feel that the answer is not very good. Hereby summarize the records. In
simple terms, pessimistic locks think that the worst will occur in everything, and optimistic locks think that everything is the most The development of a good situation corresponds to a negative and a positive.

Pessimistic lock

Has strong exclusive and exclusive characteristics. It refers to the conservative attitude that the data is modified by the outside world (including the current other transactions of the system and the transaction processing from external systems). Therefore, the data is locked during the entire data processing process. (From Baidu Encyclopedia)
Many locks in traditional relational databases use this mechanism, such as row locks, table locks, read locks, write locks, etc., which are locked before doing operations. Reentrant locks such as synchronize and ReentrantLock in Java are such mechanisms used;

Optimistic locking

I always think that there will be no concurrency problems. Every time I fetch data, I always think that no other thread will modify the data, so it will not be locked, but when updating, it will judge whether other threads have done data before. Modifications are generally implemented using version number mechanisms or CAS operations. Optimistic locks are suitable for multi-read application types, which can improve throughput. Like the write_condition mechanism provided by the database, they are actually optimistic locks. In Java, the atomic variable class under the java.util.concurrent.atomic package uses CAS, an implementation of optimistic locking.

Two ways to implement optimistic locking:

1. Version number mechanism

That is to add a version identifier to the data. In the version solution based on the database table, it is generally achieved by adding a "version" field to the database table. When reading out the data, read out this version number together, and later update, add one to this version number. At this time, the version data of the submitted data is compared with the current version information of the corresponding record of the database table, and if the version number of the submitted data is greater than the current version number of the database table, it is updated, otherwise it is considered to be expired data. The following is explained with a picture:

A bank operating system, assuming that there is a version field in the account information table in the database, the current value is 1; and the current account balance field (balance) is $ 100.

  1. Operator A now reads it (version = 1) and deducts $ 50 ($ 100- $ 50) from his account balance.
  2. During the operation of operator A, operator B also reads this user information (version = 1) and deducts $ 20 ($ 100- $ 20) from his account balance.
  3. Operator A completes the modification work, submits the data with version = 1 and the balance after account deduction (balance = $ 50) to the database for update, at this time, the data is updated because the version of the submitted data is equal to the current version of the database record, and the database record version Update to 2 (setversion = version + 1 whereversion = 1).
  4. Operator B completed the data entry operation, and also tried to submit the data with version = 1 to the database (balance = $ 80), but at this time when comparing the database record version, it was found that the version number of the data submitted by operator B was 1, the database record The current version is also 2, which does not satisfy the optimistic locking strategy of "the submitted version must be equal to the record of the current version in order to perform the update". Therefore, the submission of operator B is rejected.

In this way, the possibility of operator B overwriting the result of operator A's operation with the result of old data modification based on version = 1 is avoided.
Insert picture description here

2. CAS algorithm

Compare and swap (compare and swap) is a famous lock-free algorithm. Lock-free programming, that is, synchronization of variables between multiple threads without using locks, that is, synchronization of variables without threads being blocked, so it is also called non-blocking synchronization (Non-blocking Synchronization). CAS algorithm involves three operands

需要读写的内存值 V
进行比较的值 A
拟写入的新值 B

If and only if the value of V is equal to A, CAS uses atomic value to update the value of V, otherwise no operation will be performed (compare and replace is an atomic operation). In general, it is a spin operation, that is, continuous retry. The specific follow-up will continue to be in-depth understanding, now only briefly mentioned.

Reference blog post:
Original link: https://blog.csdn.net/qq_34337272/article/details/81072874
Original link: https://blog.csdn.net/L_BestCoder/article/details/79298417

Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/100974565