Pessimistic locking and optimistic strategies

Source: Internet
Author: User

Pessimists and optimists do things completely different, pessimistic outlook on life is a thing I have to completely control to do, otherwise I think this thing will be wrong, and optimistic outlook on the contrary, regardless of the final outcome of everything, he will first try to do, the big deal finally unsuccessful. This is the difference between pessimistic lock and optimistic lock, pessimistic lock will be the whole object lock accounted for their own before going to do the operation, optimistic lock does not get the lock directly to do the operation, and then through a certain detection means to determine whether to update the data. This section explores the optimistic locks in depth.

Synchronized mutex is a pessimistic lock, it has an obvious disadvantage, it regardless of the existence of data storage competition is locked, with the increase in concurrency, and if the lock time is longer, its performance costs will become very large. Is there a way to solve this problem? The answer is optimistic locking based on conflict detection. In this mode, there is no so-called lock concept, each thread directly first to perform operations, after the completion of the calculation to detect the existence of shared data with other threads to compete, if not to make this operation successful, if there is a competition for shared data can continue to re-perform operations and detection, until successful, can be called CAs spin.

The core algorithm for optimistic locking is CAs (Compare and swap, which compares and Swaps), which involves three operands: memory value, expected value, new value. The memory value is modified to the new value only if and only if the expected value and memory value are equal. The logic of this process is to first check if the value of a block of memory is the same as when I read it before, and if it is not the same, it means that the memory value has been changed by another thread, discarding this operation, otherwise the new value can be set to this block memory when no other thread is operating on this memory value . There are two threads that may be about a memory operation at the same time, the line Cheng read a memory value as the expected value, execute to somewhere when the line Cheng decided to set the new value into the memory block, if the thread one modifies the memory block during this period, then through the CAs can be detected, if the detection is not a problem, the line Cheng the new value

Pessimistic locking and optimistic strategies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.