What is the AtomicLong.set method in Java ?
AtomicLong represents a long value that may be updated AtomicLong is present in the java.util.concurrent.atomic package.
The set method of the AtomicLong can be used to set the value of the AtomicLong object.
Syntax
public final void set(long newValue)
Parameters
This method takes the value to be set in the AtomicLong object as an argument.
Return value
This method doesn’t return any value.
Code
The code below demonstrates how to use the set method:
import java.util.concurrent.atomic.AtomicLong;class Set{public static void main(String[] args) {AtomicLong atomicLong = new AtomicLong(10);long val = atomicLong.get();System.out.println("The value in the atomicLong object is " + val);System.out.println("\nCalling atomicLong.set(100)");atomicLong.set(100);System.out.println("\nThe value in the atomicLong object is " + atomicLong.get());}}
Explanation
Line 1: We have imported the AtomicLong class.
Line 2: Definition of class Set starts from here that shows the functionality of set method.
Line 4: Created a new object for the AtomicLong class with the name atomicLong and with the value 10.
Line 8: Used the set method of the atomicLong to set the value of the atomicLong to 100.
Line 9: Prints the value of the atomicLong object using the get method.