In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use AtomicInteger, AtomicBoolean and AtomicLong atomic classes. I hope you will get something after reading this article. Let's discuss it together.
We know that in multithreading, it is possible to get out of the thread safety problem if you are operating on instance variables. The code is as follows:
We see that the count value output by both threads is 0, which is obviously incorrect because the + operator is not an atomic operation. We can take a look at its implementation logic by breaking this operator apart.
According to the above expression count++, you have to go through three necessary steps:
The first step is to get the value of count
Calculate the value of count+1
Then assign the calculated value to count.
And we also know that the count++ operation is a post operator, that is, when dealing with this operation, JVM outputs the value first and then assigns the calculated value to the count. In other words, if we do not use multi-threading and only single-thread execution, then the result of the program has always been 0, because the + + symbol of count++ is followed by, so it is output first in the assignment, that is, add 1 to the value of count after the output.
We see that if there is only one thread, the output value of count++ must be 0, not because the + + operation is not performed, but because the + + operation is assigned after the output, so if you want to see the value after the + + operation is executed, then we can output the value of count once.
Going back to the problem of multithreading, if two or more threads perform count++ operations at the same time, their steps may be (the result of multithreading execution is not fixed) the thread gets the count value first because it is an instance variable, so JVM initializes the default value of 0. At this point, the count value obtained by the thread is 0, and because the + + operation is a post operator, JVM will first output the assignment before performing the operation. So thread one will output 0 first, but it is possible that thread two also executed count++. That is to say, thread 2 executes as soon as the thread has no output, and it executes the same process as above, first getting the count and then outputting it. It is possible that thread 2 executes faster than thread 1, so the output result may output thread 2 first and then thread 1, but their result may be 0, which shows that count++ is not an atomic operation. Let's take a look at the classes AtomicInteger, AtomicBoolean, AtomicLong with atomic operations. The method usage of all three classes is the same, so let's focus on the use of the AtomicInteger class. It provides us with a lot of methods of atomic operation. The details are as follows:
There are many methods that support atomic operations, which will not be introduced here. When you use them, you can check the relevant API documents. Below we will describe the above three methods in detail. The thread safety problem encountered above is due to the thread safety problem caused by the execution of count++ operations because it is not an atomic operation and multiple threads can execute at the same time. Now let's modify the code to support the atomic operation of the getAndIncrement () method before executing the above code to see what happens.
We see that there is no thread safety problem this time because the getAndIncrement () method is an atomic method and the thread cannot be interrupted during execution. So how does the getAndIncrement () method implement the atomic operation? let's analyze the source code of the getAndIncrement () method.
We see that the first statement in the source code is an endless loop, that is, if the assignment fails, it will always loop. The purpose of get () is to get the value in the current AtomicInteger class. Then add 1 to the value in the acquired AtomicInteger class. The most important thing is this compareAndSet method, we know that the function of this method is to compare the value in the AtomicInteger class with the expected value, if equal, then set the value in the AtomicInteger class to the value of the method parameter, and this method is also an atomic operation, there will be no thread safety problems. If the compareAndSet method returns false. It means that the value obtained by the get () method is not up-to-date, that is to say, if other threads have modified the values and in the AtomicInteger class, the method will execute the loop and continue to judge until the compareAndSet method returns ture, which is the logic of the getAndIncrement () method to implement the atomic operation. Let's move on to the source code of the addAndGet () method.
We find that the general logic of the addAndGet () method is the same as that of the getAndIncrement () method, except that one is to add one and the other is to add parameters, the specific principle is the same as the above analysis, so we will not introduce it in detail here. In short, if there are operations such as data operations in multithreading, it is best to use the atomic operation class provided by Java, which can help us solve a lot of thread safety problems.
After reading this article, I believe you have a certain understanding of "how to use AtomicInteger, AtomicBoolean and AtomicLong atomic classes". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!
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.