In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the Java thread interview questions". Interested friends may wish to have a look at them. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the Java thread interview questions?"
The function and difference of Java keyword volatile and synchronized?
There is a main memory area (Main Memory or Java Heap Memory) in JVM. All variables in Java are stored in main memory and shared by all threads.
On the other hand, each thread has its own working memory (Working Memory), which stores copies of some variables in main memory.
Thread operations on all variables do not occur in the main memory area
It happens in working memory, and threads can not access each other directly, and the transfer of variables in the program depends on the main memory.
Two keywords that can achieve visibility in java
1) use the keyword synchronized
2) use the keyword volatile
Synchronized can achieve atomicity (synchronization) and visibility of multithreading.
JVM's two provisions on Synchronized:
1) before the thread is unlocked, the latest value of the shared variable must be refreshed to the main memory.
2) when the thread is locked, the value of the shared variable in the working memory will be cleared, so when using the shared variable, you need to re-read the latest value from the main memory (note: locking and unlocking need the same lock).
Volatile can guarantee the visibility of variables, but not the atomicity of composite operations.
How does volatile achieve memory visibility?
In depth: this is achieved by adding a memory barrier and prohibiting reordering optimization.
1) when writing to the volatile variable, a store barrier instruction is added after the write operation.
2) when performing a read on the volatile variable, a load barrier instruction is added after the read operation.
Generally speaking: every time a volatile variable is accessed by a thread, it forces the value of the variable to be reread from the main memory, and when the variable changes, it forces the thread to refresh the latest value to the main memory, so that different threads can always see the latest value of the variable at any time.
The process by which a thread writes a volatile variable:
1) change the value of the copy of the volatile variable in the working memory of the thread.
2) refresh the value of the changed copy from working memory to main memory.
The process by which a thread reads the volatile variable:
1) read the latest value of the volatile variable from the main memory into the thread's working memory.
2) read a copy of the volatile variable from working memory
Volatile cannot guarantee the atomicity of compound operation of volatile variables.
Use volatile and synchronized for the following program
Private int number = 0
Number++;// is not an atomic operation
1 read the value of number
2 add 1 to the value of number
3 write the value of the latest number
/ / join synchronized and become an atomic operation
Synchronized (thhis) {
Number++
}
/ / changed to volatile variable, cannot guarantee atomicity
Private volatile int number = 0
Volatile variables can be used to provide thread safety, but can only be applied to a very limited set of use cases: there are no constraints between multiple variables or between the current and modified values of a variable. Therefore, using volatile alone is not enough to implement counters, mutexes, or any class with invariants (Invariants) associated with multiple variables (such as "start")
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.