Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

atomiclong
set
java
communitycreator

What is the AtomicLong.set method in Java ?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

AtomicLong represents a long value that may be updated atomicallyAtomic operation is performing a single unit of work on a resource. During that operation, no other operations are allowed on the same resource until the performing operation is finished.. The 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());
}
}
set() method changes the value of atomicLong.

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.

RELATED TAGS

atomiclong
set
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring