In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "case analysis of Java concurrent interview questions". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Java concurrent interview question case analysis".
Title
Conclusion
Multithreading concurrently carries out set and get operations at the same time, A thread calls set method, B thread can certainly see this change!
Analysis.
This class is very simple, there is a property, there are two methods: get, set method, one is used to set the property value, one is used to get the property value, and the synchronized is added to the setting property method.
Implicit message: multi-thread concurrent set, get operations at the same time, A thread calls set method, B thread can be perceived inside?
At this point, the question becomes whether synchronized can guarantee visibility in the context just mentioned!
The usage of the keyword synchronized
Specify locked object: lock a given object, and you need to acquire a lock for a given object before entering the synchronization code.
Act directly on the instance method: it is equivalent to locking the current instance and obtaining the lock of the current instance before entering the synchronization code.
Acting directly on static methods: it is equivalent to locking the current class and acquiring the lock of the current class before entering the synchronization code.
Synchronized's job is to lock code that needs to be synchronized so that only one thread at a time can enter the synchronization block (which is actually a pessimistic strategy) to ensure security between threads.
From here we can know that what we need to analyze belongs to the second kind of situation, that is, if multiple threads carry out set method at the same time, because of the lock, they will perform set operation one by one, and it is thread safe, but the get method does not add a lock, which means that if A thread is carrying out set while B thread can carry out get operation. And multiple threads can perform get operations at the same time, but there can be at most one set operation at a time.
Java memory model happens-before principle
The JSR-133 memory model uses the concept of happens-before to illustrate memory visibility between operations. In JMM, if the result of one operation needs to be visible to another operation, then there must be a happens-before relationship between the two operations. The two operations mentioned here can be either within one thread or between different threads.
Happens-before rules that are closely related to programmers are as follows:
Program order rules: each operation in a thread, happens-before any subsequent operations in that thread.
Monitor lock rule: unlock a monitor, and happens-before then locks the monitor.
Volatile variable rule: write to a volatile domain, happens-before any subsequent reads to that volatile domain.
Transitivity: if A happens-before B and B happens-before C, then A happens-before C.
Note that just because there is a happens-before relationship between two operations does not mean that the previous operation must be performed before the latter one! Happens-before only requires the previous operation (the result of execution) to be visible to the latter operation, and the previous operation comes before the second operation in order (the first is visible to and ordered before the second).
There are monitor lock rules: unlock a monitor, and happens-before then locks the monitor. This one is only for the set method of synchronized, but there is no explanation for get.
In fact, in this context, a synchronized set method, an ordinary get method, a thread calls the set method, b thread can certainly be visible to this change!
Volatilevolatile visibility
The happens-before principle mentioned earlier: the volatile variable rule: writing to a volatile domain, happens-before to any subsequent reading of the volatile domain. Volatile thus ensures the visibility in multithreading!
Volatile forbids memory reordering
The following is a table of volatile reordering rules developed by JMM for the compiler:
In order to implement the memory semantics of volatile, when generating bytecode, the compiler inserts a memory barrier in the instruction sequence to prevent certain types of processors from reordering.
The following is the JMM memory barrier insertion strategy based on the conservative policy:
Insert a StoreStore barrier in front of each volatile write operation.
Insert a StoreLoad barrier after each volatile write operation.
Insert a LoadLoad barrier after each volatile read operation.
Insert a LoadStore barrier after each volatile read operation.
The following is a schematic diagram of the instruction sequence generated after the volatile write operation is inserted into the memory barrier under a conservative strategy:
The following is a schematic diagram of the instruction sequence generated after the volatile read operation is inserted into the memory barrier under a conservative strategy:
The memory barrier insertion strategy for the above volatile write and volatile read operations is very conservative. In actual execution, the compiler can omit unnecessary barriers according to specific circumstances, as long as the write-read memory semantics of volatile is not changed.
Simulation
Through the above analysis, in fact, all the contents involved in this question have been mentioned and answered.
Although you know the reason, it is not easy to simulate! Let's simulate and see the effect:
PublicclassThreadSafeCache {intresult;publicintgetResult () {returnresult;} publicsynchronizedvoidsetResult (intresult) {this.result = result;} publicstaticvoidmain (String [] args) {ThreadSafeCache threadSafeCache = newThreadSafeCache (); for (inti = 0; I {intx = 0 * while (threadSafeCache.getResult ())
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.