In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the three cores of Java thread safety". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the three cores of Java thread safety.
Before we begin, let's take a look at what thread safety is.
Thread safety
Thread safety can be understood simply as a method or an instance that can be used in a multithreaded environment without problems.
Running multiple threads in the same program does not in itself cause a problem; the problem is that multiple threads access the same resource. For example, the same memory area (variable, array, or object), system (database, web services, etc.), or file. In fact, these problems can only occur when one or more threads write to these resources, and as long as the resources do not change, it is safe for multiple threads to read the same resources.
For example, the following code may cause thread unsafe problems in a multithreaded environment:
Imagine that threads An and B execute the same code at the same time, and we don't know when the operating system will switch between the two threads. JVM does not execute this code as a single instruction.
The + + operation is not an atomic operation, and the same-the operation is not an atomic operation.
Atomicity
Atomicity means that the operation is inseparable. Its performance lies in that some operations of shared variables should be inseparable and must be completed continuously.
For example, the + + operation above is actually done in three steps by JMM.
1. Read the value of the variable xttblog
2. The value of xttblog + 1
3. Assign the value to the variable xttblog
If the value of xttblog is tampered with during any of these three operations, there will be results that we do not want. So we have to make sure it's atomic.
So if you want to implement atomic operations like + +, you need to use synchronized or lock for locking.
If it is the self-increment operation of the basic class, it can be implemented using an atomic class such as AtomicInteger (which is essentially accomplished by using the CAS instruction at the CPU level).
Visibility
Visibility refers to whether changes made by one thread to a shared variable are visible to another thread.
Why is there such a problem?
We know that java thread communication communicates through shared memory, and we know that in order to speed up execution, threads generally do not directly operate on memory, but on caching.
Java thread memory model:
In fact, threads operate on their own working memory, not on main memory directly. If the thread's operation on the variable is not refreshed and written to the main memory, it only changes the copy of the variable in its own working memory, then it is not visible to other threads. If another variable does not read the latest value in main memory, but uses the old value, the same can be listed as invisible. (can understand the phantom reading in the database, dirty reading)
For jvm, main memory is a java heap shared by all threads, while copies of shared variables in working memory are copied from main memory and are thread-private local variables in the java stack.
So how do we know when the variables of working memory will be written to the main memory?
The volatile keyword is used to ensure memory visibility. When thread A updates the volatile-decorated variable, it immediately flushes to the main thread and empties the value of the variable in the rest of the cache, so that the remaining threads can only go to main memory to read the latest values.
Variables modified with the volatile keyword get the latest data each time they are read, and no matter which thread makes changes to the variable, it is immediately flushed to main memory.
Synchronized and locking can also guarantee visibility, based on the fact that the rest of the thread cannot access the shared variable until the lock is released. But compared with volatile, it is more expensive.
In addition, happens-before exists in Java. 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. We'll talk about this later!
Order
Orderliness means that when the program is executed, the order of the code execution of the program is consistent with the order of statements.
Why are there inconsistencies?
This is due to reordering.
In the Java memory model, compilers and processors are allowed to reorder instructions, but reordering is not a problem in a single thread, but data inconsistencies may occur in multiple threads.
For example, the following code:
Normally, the order of execution should be 1 > 2 > 3. But sometimes JVM will rearrange instructions in order to improve the overall efficiency, so the order of execution may be 2 > > 1 > > 3. But JVM can't rearrange everything, it can only be rearranged if the final result is consistent with the code execution result.
Rescheduling will not be a problem in a single thread, but there will be data inconsistencies in multiple threads.
Volatile can be used in Java to ensure orderliness, and synchronized and lock can also ensure orderliness, which can only be accessed by one thread at a time in the same way as atomicity.
In addition to explicitly ensuring the order through the volatile keyword, JVM also implicitly guarantees the order through the happen-before principle.
One of them is for the volatile keyword, and the write operation for the volatile keyword must be before the read operation, that is, the read value must be up to date.
At this point, I believe you have a deeper understanding of "what are the three cores of Java thread safety". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.