In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the locks in Java". In the daily operation, I believe that many people have doubts about the locks in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the locks in Java?" Next, please follow the editor to study!
Type of lock
The locks in Java are divided into pessimistic locks and optimistic locks from a macro point of view.
Optimistic lock
Optimistic lock is a kind of optimistic thinking, that is, reading more and writing less, the possibility of concurrent writing is low, and every time you go to get the data, you think that others will not modify it, so you will not lock it. However, when updating, you will judge whether others have updated the data during this period. Read out the current version number first when writing, and then add the lock operation (compare with the previous version number, update if the same). Repeat the read-compare-write operation if it fails.
Optimistic locks in java are basically achieved through CAS operations. CAS is an updated atomic operation. If the current value is the same as the input value, it will be updated, otherwise it will fail.
Pessimistic lock
Pessimistic lock is pessimistic thinking, that is, if there are too many writes, the possibility of concurrent writing is high, and every time you go to get the data, you think that others will modify it, so you will lock it every time you read and write the data, so that others will block until they get the lock if they want to read and write the data. The pessimistic lock in java is that under the synchronized,AQS framework, the lock is first tried to acquire the lock by cas optimistic lock, and then it will be converted to pessimistic lock, such as RetreenLock.
Second, the cost of Java thread blocking
The thread of Java is mapped to the native thread of the operating system. If you want to block or wake up a thread, you need the operating system to intervene and switch between the user mode and the kernel state. This switch will consume a lot of system resources, because both the user mode and the kernel state have their own dedicated memory space, special registers and so on. Switching from user mode to kernel mode requires passing many variables and parameters to the kernel. The kernel also needs to protect some register values and variables when switching in the user mode, so that the kernel mode can switch back to the user mode to continue to work after the kernel mode call is over.
If thread state switching is a high frequency operation, this will consume a lot of CPU processing time
For simple blocks of code that require synchronization, it would be unwise to choose a synchronization strategy that takes longer to acquire a lock pending operation than the user code takes to execute.
Synchronized will cause threads that cannot be locked to enter a blocking state, so it is a heavyweight synchronous manipulation in Java language, which is called heavyweight lock. In order to alleviate the above performance problems, JVM has introduced lightweight lock and bias lock from 1.5. spin lock is enabled by default, and they all belong to optimistic lock.
Knowing the cost of java thread switching is one of the bases for understanding the advantages and disadvantages of various locks in java.
III. Markword
Markword is a part of the java object data structure, and the markword of the object is closely related to various types of java locks.
The length of markword data is 32bit and 64bit in 32-bit and 64-bit virtual machines (with compression pointer not enabled). Its last 2bit is the lock status flag bit, which is used to mark the state of the current object, and the state of the object determines the content of markword storage, as shown in the following table:
State flag bit storage content unlocked 01 object hash code, object generation age lightweight lock 00 pointer inflation to lock record (heavyweight lock) 10 pointer to perform heavyweight locking GC flag 11 null (no recording information required) can be biased towards 01 thread ID, bias timestamp, object generation age
The markword structure of a 32-bit virtual machine in different states is as follows:
Lock status 25bit4bit1bit2bit
Whether 23bit2bit is biased towards lock flag bit
The lightweight lock points to the pointer 00 of the lock record in the stack
A pointer to a mutex (heavyweight lock) 10
GC tag empty 11
Prefer lock thread IDEpoch object generation age 101The hashCode object generation age of unlocked object is 0001
Summary
The four locks of java are mentioned earlier, they are heavyweight locks, spin locks, lightweight locks and bias locks. Different locks have different characteristics. Each lock will perform well only in its specific scenario. No lock in java can be efficient in all cases. The reason for introducing so many locks is to deal with different situations.
Heavyweight lock is a kind of pessimistic lock, while spin lock, lightweight lock and bias lock belong to optimistic lock.
At this point, the study of "what are the locks in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.