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 will explain in detail how to implement the atomic operation class in Java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
I. introduction
Add the java.util.concurrent (J.U.C) package to JDK1.5, which is based on CAS. CAS is a common implementation of non-blocking algorithm, and its performance is better than that of synchronized.
Low-level support is introduced into DK1.5, CAS operations are exposed on int, long and object references, and JVM compiles them into the most efficient methods provided by the underlying hardware. On the platform running CAS, the runtime compiles them into corresponding machine instructions. All atomic variable types under the java.util.concurrent.atomic package, such as AtomicInteger, use these underlying JVM support to provide an efficient CAS operation for reference types of numeric types.
2. Introduction of Java atomic class
The 13 classes in the atomic package belong to 4 types of atomic update methods.
(1) basic types of atomic updates
(2) Atomic update array
(3) Atomic update reference
(4) Atomic update attributes
The classes in the atomic package are basically wrapper classes implemented in Unsafe.
1. Basic types of atomic updates
AtomicBoolean: atomic update Boolean type.
AtomicInteger: atomic update integers.
AtomicLong: atomic update long integers.
This class is explained in AtomicInteger:
AtomicInteger is an Integer class that supports atomic operations, which ensures that the addition and subtraction of AtomicInteger type variables are atomic, and there will be no data inconsistencies across multiple threads. If you do not use AtomicInteger, to implement a sequential ID, you must lock each acquisition to avoid the phenomenon of getting the same ID when concurrency occurs.
Int addAndGet (int delta) atomically adds the input value to the value in the instance and returns the result
Boolean compareAndSet (int expect, int update) if the value entered is equal to the expected value, the value is set to the value entered in an atomic way
Int getAndIncrement () adds 1 to the current value atomically. Note that what is returned here is the value before increment.
Void lazySet (int newValue) will eventually be set to newValue. Setting the value with lazySet may cause other threads to read the old value for a short period of time.
Int getAndSet (int newValue) is atomically set to the value of newValue and returns the old value.
So how does getAndIncrement implement atomic operations?
1public final int getAndIncrement () {
2 for (;;) {
3 int current = get ()
4 int next = current + 1
5 if (compareAndSet (current, next))
6 return current
7}
8} about how to implement the atomic operation class in Java is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.