Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of AtomicInteger Atomic Integer Syntax in java concurrent JUC Toolkit

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the java concurrent JUC toolkit AtomicInteger atomic integer syntax example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

The AtomicInteger class stores an int value underneath and provides methods to perform atomic operations on the int value. AtomicInteger was introduced as part of the java.util.concurrent.atomic package since Java 1.5.

1. Basic usage of AtomicInteger

Using the AtomicInteger constructor below, you can create an AtomicInteger object whose initial value defaults to 0. AtomicInteger provides get and set methods to get the underlying int integer value and set the int integer value

/ / atomicInteger object with an initial value of 0 AtomicInteger atomicInteger = new AtomicInteger (); / / atomicInteger object with an initial value of 200 AtomicInteger atomicInteger = new AtomicInteger; int currentValue = atomicInteger.get (); / / 100atomicInteger.set (2453); / / the current value is 2453

But the above method is not its core content for AtomicInteger, the core content of AtomicInteger is reflected in its atomicity, which we will introduce below.

two。 When do you need to use AtomicInteger

We usually use AtomicInteger in the following two scenarios

To operate a counter in a multithreaded concurrency scenario, it is necessary to ensure the atomicity of the counter operation.

Carry on the numerical comparison, if the given value is equal to the current value, update the value, and realize the non-blocking algorithm of the operation.

2.1. Atomic counter scene

Using AtomicInteger as a counter, AtomicInteger provides several methods for atomic operations of addition and subtraction.

For example, to get a value from a map, use the get () method, which is the first operation; after getting the value, add n to the value, which is the second operation; and put the added value into the map again is the third operation. The atomicity of the operation means that in the scenario of multi-thread concurrency, the above three operations are atomic, that is, inseparable. There will not be A thread get value, B thread also get to this value, two threads at the same time for the value of the operation and then put in again, this situation will not occur for AtomicInteger, AtomicInteger operation is thread-safe, indivisible.

AddAndGet ()-adds the given value to the current value and returns the new value after addition, ensuring the atomicity of the operation.

GetAndAdd ()-adds the given value to the current value, returns the old value, and guarantees the atomicity of the operation.

IncrementAndGet ()-increases the current value by 1 and returns the new value after the increase. It is equivalent to the + + I operation and ensures the atomicity of the operation.

GetAndIncrement ()-increments the current value by 1 and returns the old value. It is equivalent to the + I operation and ensures the atomicity of the operation.

DecrementAndGet ()-subtracts the current value from 1 and returns the new value after subtraction, which is equivalent to the iMel-operation and ensures the atomicity of the operation.

GetAndDecrement ()-subtracts the current value from 1 and returns the old value. It is equivalent to the-I operation and ensures the atomicity of the operation.

Here is an example of the atomic operation method of AtomicInteger

Public class Main {public static void main (String [] args) {/ / atomicInteger AtomicInteger atomicInteger = new AtomicInteger with an initial value of 100; System.out.println (atomicInteger.addAndGet (2)); / / plus 2 and return 102 System.out.println (atomicInteger); / / 102 System.out.println (atomicInteger.getAndAdd (2)) / / get 102and then add 2 System.out.println (atomicInteger); / / 104System.out.println (atomicInteger.incrementAndGet ()); / / add 1 to get 105System.out.println (atomicInteger); / / 105System.out.println (atomicInteger.getAndIncrement ()) / / get 105and then add 1 System.out.println (atomicInteger); / / 106System.out.println (atomicInteger.decrementAndGet ()); / / minus 1 to get 105System.out.println (atomicInteger); / / 105System.out.println (atomicInteger.getAndDecrement ()); / / get 105and then subtract 1 System.out.println (atomicInteger) / / 104}} 2.2. Numerical comparison and exchange operation

The compareAndSet operation compares the contents of a memory location with a given value, and only if they are the same will the contents of that memory location be modified to a given new value. This process is done in a single atomic operation.

CompareAndSet method: if the current value = = expected value, set the value to the given update value.

Boolean compareAndSet (int expect, int update)

Expect is the expected value

Update is the updated value

Example of the AtomicInteger compareAndSet () method

Import java.util.concurrent.atomic.AtomicInteger;public class Main {public static void main (String [] args) {/ / atomicInteger AtomicInteger atomicInteger= new AtomicInteger (100) with an initial value of 100; / / current value 100 = expected value 100, so set atomicInteger=110 boolean isSuccess = atomicInteger.compareAndSet (100110); System.out.println (isSuccess) / / if the output result is true, the operation is successful / / the current value is 110 = the expected value is 100? Not equal, so atomicInteger is still equal to 110isSuccess = atomicInteger.compareAndSet (100120); System.out.println (isSuccess); / / the output result is false indicating operation failure}} 3. Summary

AtomicInteger can help us to achieve thread safety and atomicity of int numerical operations in multithreaded scenarios without using synchronized synchronization locks. And using AtomicInteger to implement atomic manipulation of int values is far more efficient than using synchronized synchronous locks.

Java.util.concurrent.atomic package not only provides us with AtomicInteger, but also provides AtomicBoolean Boolean atomic operation class, AtomicLong long integer Boolean atomic operation class, AtomicReference object atomic operation class, AtomicIntegerArray integer array atomic operation class, AtomicLongArray long integer array atomic operation class, AtomicReferenceArray object array atomic operation class.

Thank you for reading this article carefully. I hope the article "sample Analysis of Atomic Integer Grammar of java concurrent JUC Toolkit AtomicInteger" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report