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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)