search
HomeJavaJavaInterview questionsJava interview - optimistic locking and pessimistic locking

Java interview - optimistic locking and pessimistic locking

Nov 27, 2020 pm 03:38 PM
javaoptimistic lockingpessimistic lockinterview

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};

(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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.