AtomicLongArray set() method in Java with Examples7 Jan 2025 | 3 min read A 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 |
Combining items of two arrays in place is a common difficulty when merging them without requiring extra space. To ensure that elements from both arrays are sorted and arranged correctly without requiring the use of an additional array for storage, this requires careful manipulation. Method 1:...
8 min read
A key idea in object-oriented programming is polymorphism, which enables objects of various kinds to be considered instances of a single superclass or interface. Java has two methods for achieving polymorphism: static polymorphism (sometimes called compile-time polymorphism) and dynamic polymorphism (often called runtime polymorphism). The...
4 min read
JonDonym also known as (JAP), is the proxy system that is made to allow Web browsing with the revocable pseudonymity (the state of using or being published under a pseudonym, a false or fictitious name). Without this anonymity, every device that is using the internet...
4 min read
Software development requires logging, which is also necessary for debugging and troubleshooting. The Logger class in Java is a crucial tool for logging data and is utilised extensively in many applications. The Java standard library includes the Logger class, which offers a straightforward and adaptable mechanism...
4 min read
? In Java, converting a string to a timestamp involves parsing the string representation of a date and time into a java.sql.Timestamp object. The process is commonly required when dealing with date and time data obtained from external sources or user inputs. In this section, we will...
3 min read
In Java, the design principles are the set of advice used as rules in design making. In Java, the design principles are similar to the design patterns concept. The only difference between the design principle and design pattern is that the design principles are more generalized...
5 min read
Dead code is a common issue that developers encounter in their programming journey. It refers to lines or blocks of code that are written but never executed during the program's runtime. While this may seem harmless, dead code can clutter a codebase, making it harder to...
3 min read
The java.text.ChoiceFormat is a class containing a format() as a function. To obtain the appended string builder of the format value of a specific limit value given as a parameter and text passed as a parameter in this method, utilize the ChoiceFormat class. Syntax: public StringBuffer format(double...
4 min read
Java, a versatile and item-oriented programming language, employs an idea referred to as approach binding. Method binding refers to the process of connecting a way name to the actual technique implementation. There are types of method binding in Java: static binding and dynamic binding. What is method...
4 min read
In Java, when we use a double data type before a variable it represents 15 digits after the decimal point. But sometimes we require only two decimal places after decimal points like to represent rupees and other units. So, in this section, we are going to...
3 min read
We request you to subscribe our newsletter for upcoming updates.
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