AtomicLongArray set() method in Java with ExamplesA built-in Java method that allows you to set a value at any location in the AtomicLongArray is called Java.util.concurrent.atomic.AtomicLongArray.set(). This function modifies the value at that index by accepting as an argument the AtomicLongArray's index value. There is absolutely nothing that this method returns. The functions set() and getAndSet() are comparable. However, the later returns the value at the specified index before setting the new value at that index, while the former returns no value at all. Syntax: Parameters: Two parameters are required by the function: i - the value of the index that has to be updated. newValue - the updated value to be added to the index. Return Value: There is no value returned by the function. Example 1:The code shows how to use Java's AtomicLongArray. After initialization, an AtomicLongArray with the name AtLongarr is created from an array of long integers. This atomic array's initial contents are printed. The code then uses the set function to update the value at a specified index (in this case, index 5) to a new value (12). The modified AtomicLongArray, which displays the array's modification, is printed at the end. Because it guarantees thread-safe operations on the array items, the AtomicLongArray is helpful in circumstances involving concurrent programming. Implementation:FileName: SetAtomicExample1.java Output: The array given is : [1, 3, 5, 7, 9, 11, 13, 15] The array after update given is : [1, 3, 5, 7, 9, 12, 13, 15] Example 2:The Java code shows how to create and edit an AtomicLongArray. First, an AtomicLongArray called AtLongarr is initialized using a standard array of long integers that is defined. This atomic array's initial contents are printed. Next, using the set method, an attempt is made to update the value at index 9 to 100. However, since the array only has 8 members, index 9 is illegal, and this will raise an IndexOutOfBoundsException. The wrong index emphasizes how crucial it is to make sure appropriate indices are used in concurrent programming, even though the original goal was to demonstrate thread-safe actions on array components. Implementation:FileName: SetAtomicExample2.java Output: The array given is : [1, 3, 5, 7, 9, 11, 13, 15] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 8 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.VarHandleLongs$Array.setVolatile(VarHandleLongs.java:776) at java.base/java.util.concurrent.atomic.AtomicLongArray.set(AtomicLongArray.java:106) at SetAtomicExample1.main(SetAtomicExample1.java:18) Next TopicCan-we-extend-final-method-in-java |
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