AtomicIntegerArray set() method in Java with ExamplesA built-in Java function called Java.util.concurrent.atomic.AtomicIntegerArray.set() allows you to set a value at any point in the AtomicIntegerArray. This function modifies the value at the specified index by providing the AtomicIntegerArray's index value as an argument. There is no value returned by the above method. Similar to getAndSet(), the set() function returns a value at the specified index before setting a new value there. In contrast, the former function returns no result at all. Syntax: Parameters: The function takes two parameters: i - the value of the index that has to be updated. newValue - the updated value to be applied to the index. Return Value: There is no value returned by the function. Example 1:The provided Java code shows how to utilize AtomicIntegerArray, a thread-safe integer array. First, an initial integer array called arr is formed. The AtomicIntegerArray atintArray is then initialized using this array. The AtomicIntegerArray's initial value is printed to the console by the code. Next, it uses AtomicIntegerArray's set function to update the value at index 7 to 100. To display the change, the updated array is printed at the end. This example demonstrates how safe modifications to individual elements can be made in a concurrent context using AtomicIntegerArray. Implementation:FileName: SetIntegerExample1.java Output: The array is given by : [10, 20, 30, 40, 50, 60, 70, 80, 90] The array after the update is given by : [10, 20, 30, 40, 50, 60, 70, 100, 90] Example 2:The Java code that is provided shows how to utilize AtomicIntegerArray to perform thread-safe operations on an integer array. Initializing an integer array arr with certain values results in the creation of an AtomicIntegerArray called atintArray from this array. The code will show the object's memory address rather than the values even though it is trying to output the original AtomicIntegerArray. Using the set method, it then attempts to alter the value at index 9 to 100. But since there are only 9 members in the array (indices 0 through 8), this will throw an IndexOutOfBoundsException. Implementation:FileName: SetIntegerExample2.java Output: The array is given by : [10, 20, 30, 40, 50, 60, 70, 80, 90] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 9 at java.base/jdk.internal.util.Preconditions$2.apply(Preconditions.java:63) at java.base/jdk.internal.util.Preconditions$2.apply(Preconditions.java:60) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.invoke.VarHandleInts$Array.setVolatile(VarHandleInts.java:776) at java.base/java.util.concurrent.atomic.AtomicIntegerArray.set(AtomicIntegerArray.java:106) at SetIntegerExample1.main(SetIntegerExample1.java:19) |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India