In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what types of locks are in the data". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Pessimism lock Pessimism lock refers to a conservative attitude towards data being modified by the outside world, thinking that the data will be easily modified by other threads, so the data is locked before the data is processed, and the data is locked throughout the data processing process. Pessimism locks are mainly divided into shared locks and exclusive locks. Shared lock is also known as read lock, referred to as S lock. As the name implies, shared locks are multiple transactions that can share a lock on the same data, and all can access the data, but can only read and cannot modify it. 2. Exclusive lock is also called write lock, referred to as X lock. As the name implies, exclusive locks cannot coexist with other locks. If a transaction acquires an exclusive lock on a data row, other transactions cannot acquire other locks on that row, including shared locks and exclusive locks, but the transaction that acquires the exclusive lock can read and modify the data row. For example: synchronized keyword in java, implementation class of interface Lock, select of mysql... for update etc. 2. Optimistic lock is relative to pessimistic lock, it believes that data in general will not cause conflict, so before accessing records will not be exclusive lock, but in the data commit update, will formally detect whether the data conflict. For example: using data version (Version++) recording mechanism implementation, using timestamp, CAS of Atomic in java, etc. 3. Fair lock Fair lock means that the order in which threads acquire locks is determined according to the time when threads request locks, that is, the thread that requests locks earliest will acquire locks earliest. For example: ReentrantLock pairLock = new ReentrantLock (true);4. Unfair locks Unfair locks indicate that threads do not acquire locks in a first-come, first-served order. ReentrantLock unpairLock = new ReentrantLock (false). If the constructor passes no arguments, the default is an unfair lock. 5. An exclusive lock is one thread that can acquire the lock at any time. For example: Java ReentrantLock6. Shared locks are locks that can be held by multiple threads simultaneously, allowing a resource to be read by multiple threads simultaneously. For example: ReentrantReadWriteLock 7. A reentrant lock, also known as a recursive lock, means that when the same thread acquires a lock on an outer method, it automatically acquires a lock on an inner method. The idea is to maintain a thread flag inside the lock, indicating which thread the lock is currently occupied by, and then associate a counter. The initial counter value is 0, indicating that the lock is not occupied by any thread. When a thread acquires the lock, the counter value becomes 1, and when another thread acquires the lock, it finds that the lock owner is not itself and is blocked. public sychnozied void test() { xxxxxx; test2();}public sychronized void test2() { yyyyy;}8. A spinlock is when a thread acquires a lock, and if the lock is held by another thread, the current thread will loop and wait until it acquires the lock. "What types of locks are in the data" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.