Java interview - optimistic locking and pessimistic locking

王林
Release: 2020-11-27 15:38:07
forward
9904 people have browsed it

Java interview - optimistic locking and pessimistic locking

Interview question:

The difference between optimistic locking and pessimistic locking

(Learning video sharing:java teaching video)

1. Optimistic Lock

I always think that there will be no concurrency problems. Every time I go to fetch data, I always think that no other thread will modify the data, so it will not be locked, but When updating, it will be judged whether other threads have modified the data before. Generally, the version number mechanism or CAS operation is used to implement the

version method:

is usually added to the data table A data version number version field indicates the number of times the data has been modified. When the data is modified, the version value will be incremented by one. When thread A wants to update the data value, it will also read the version value while reading the data. When submitting the update, update it only if the version value just read is equal to the version value in the current database, otherwise try again. Update operation until the update is successful.

update table set x=x+1, version=version+1 where id=#{id} and version=#{version};
Copy after login

(Recommended related interview questions:java interview questions and answers)

CAS operation method:

That is, compare and swap or compare and set, Involves three operands, the memory value where the data is located, the expected value, and the new value. When it needs to be updated, it is judged whether the current memory value is equal to the previously obtained value. If they are equal, update it with the new value. If it fails, try again. Generally, it is a spin operation, that is, continuous retry.

2. Pessimistic lock

Always assume the worst case scenario. Every time you fetch data, you think that other threads will modify it, so you will lock it (read lock, write lock, row lock, etc. ), when other threads want to access data, they need to block and suspend. It can be implemented by relying on the database, such as row locks, read locks and write locks, etc., which are all locked before operation. In Java, the idea of synchronized is also pessimistic locking.

3. Applicable Scenarios

Pessimistic lock: It is more suitable for scenarios with frequent write operations. If there are a large number of read operations, locking will be performed every time it is read. This will Adding a lot of lock overhead reduces the system throughput.

Optimistic lock: It is more suitable for scenarios where read operations are more frequent. If a large number of write operations occur, the possibility of data conflicts will increase. In order to ensure the consistency of the data, the application layer needs to continuously Re-obtaining data will increase a large number of query operations and reduce the throughput of the system.

Summary: Both have their own advantages and disadvantages. Optimistic locks are used for frequent reads, and pessimistic locks are used for frequent writes.

Related recommendations:java introductory tutorial

The above is the detailed content of Java interview - optimistic locking and pessimistic locking. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!