- Java atomiclong to long
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Number
- Methods inherited from class java.lang.Object
- Constructor Detail
- AtomicLong
- AtomicLong
- Method Detail
- get
- set
- lazySet
- getAndSet
- compareAndSet
- weakCompareAndSet
- getAndIncrement
- getAndDecrement
- getAndAdd
- incrementAndGet
- decrementAndGet
- addAndGet
- getAndUpdate
- updateAndGet
- getAndAccumulate
- accumulateAndGet
- toString
- intValue
- Java atomiclong to long
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Number
- Methods inherited from class java.lang.Object
- Constructor Detail
- AtomicLong
- AtomicLong
- Method Detail
- get
- set
- lazySet
- getAndSet
- compareAndSet
- weakCompareAndSet
- getAndIncrement
- getAndDecrement
- getAndAdd
- incrementAndGet
- decrementAndGet
- addAndGet
- getAndUpdate
- updateAndGet
- getAndAccumulate
- accumulateAndGet
- toString
- intValue
- AtomicLong
- Creating an AtomicLong
- Getting the AtomicLong Value
- Setting the AtomicLong Value
- Compare and Set the AtomicLong Value
- Adding to the AtomicLong Value
- Subtracting From the AtomicLong Value
- Using an AtomicLong as a Counter in a Lambda Expression
Java atomiclong to long
A long value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicLong is used in applications such as atomically incremented sequence numbers, and cannot be used as a replacement for a Long . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
Constructor Summary
Method Summary
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.
Atomically updates the current value with the results of applying the given function, returning the previous value.
Atomically updates the current value with the results of applying the given function, returning the updated value.
Methods inherited from class java.lang.Number
Methods inherited from class java.lang.Object
Constructor Detail
AtomicLong
public AtomicLong(long initialValue)
AtomicLong
Method Detail
get
set
public final void set(long newValue)
lazySet
public final void lazySet(long newValue)
getAndSet
public final long getAndSet(long newValue)
compareAndSet
public final boolean compareAndSet(long expect, long update)
weakCompareAndSet
public final boolean weakCompareAndSet(long expect, long update)
Atomically sets the value to the given updated value if the current value == the expected value. May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet .
getAndIncrement
public final long getAndIncrement()
getAndDecrement
public final long getAndDecrement()
getAndAdd
public final long getAndAdd(long delta)
incrementAndGet
public final long incrementAndGet()
decrementAndGet
public final long decrementAndGet()
addAndGet
public final long addAndGet(long delta)
getAndUpdate
Atomically updates the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
updateAndGet
Atomically updates the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
getAndAccumulate
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
accumulateAndGet
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
toString
intValue
Java atomiclong to long
A long value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicLong is used in applications such as atomically incremented sequence numbers, and cannot be used as a replacement for a Long . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
Constructor Summary
Method Summary
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.
Atomically updates the current value with the results of applying the given function, returning the previous value.
Atomically updates the current value with the results of applying the given function, returning the updated value.
Methods inherited from class java.lang.Number
Methods inherited from class java.lang.Object
Constructor Detail
AtomicLong
public AtomicLong(long initialValue)
AtomicLong
Method Detail
get
set
public final void set(long newValue)
lazySet
public final void lazySet(long newValue)
getAndSet
public final long getAndSet(long newValue)
compareAndSet
public final boolean compareAndSet(long expect, long update)
weakCompareAndSet
public final boolean weakCompareAndSet(long expect, long update)
Atomically sets the value to the given updated value if the current value == the expected value. May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet .
getAndIncrement
public final long getAndIncrement()
getAndDecrement
public final long getAndDecrement()
getAndAdd
public final long getAndAdd(long delta)
incrementAndGet
public final long incrementAndGet()
decrementAndGet
public final long decrementAndGet()
addAndGet
public final long addAndGet(long delta)
getAndUpdate
Atomically updates the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
updateAndGet
Atomically updates the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
getAndAccumulate
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
accumulateAndGet
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
toString
intValue
AtomicLong
The AtomicLong class provides you with a long variable which can be read and written atomically, and which also contains advanced atomic operations like compareAndSet() . The AtomicLong class is located in the java.util.concurrent.atomic package, so the full class name is java.util.concurrent.atomic.AtomicLong . This text describes the version of AtomicLong found in Java 8, but the first version was added in Java 5.
The reasoning behind the AtomicLong design is explained in my Java Concurrency tutorial in the text about Compare and Swap.
Creating an AtomicLong
Creating an AtomicLong is done like this:
AtomicLong atomicLong = new AtomicLong();
This example creates an AtomicLong with the initial value 0 .
If you want to create an AtomicLong with an initial value, you can do so like this:
AtomicLong atomicLong = new AtomicLong(123);
This example passes a value of 123 as parameter to the AtomicLong contructor, which sets the initial value of the AtomicLong instance to 123 .
Getting the AtomicLong Value
You can get the value of an AtomicLong instance via the get() method. Here is an AtomicLong.get() example:
AtomicLong atomicLong = new AtomicLong(123); long theValue = atomicLong.get();
Setting the AtomicLong Value
You can set the value of an AtomicLong instance via the set() method. Here is an AtomicLong.set() example:
AtomicLong atomicLong = new AtomicLong(123); atomicLong.set(234);
This example creates an AtomicLong example with an initial value of 123, and then sets its value to 234 in the next line.
Compare and Set the AtomicLong Value
The AtomicLong class also has an atomic compareAndSet() method. This method compares the current value of the AtomicLong instance to an expected value, and if the two values are equal, sets a new value for the AtomicLong instance. Here is an AtomicLong.compareAndSet() example:
AtomicLong atomicLong = new AtomicLong(123); long expectedValue = 123; long newValue = 234; atomicLong.compareAndSet(expectedValue, newValue);
This example first creates an AtomicLong instance with an initial value of 123 . Then it compares the value of the AtomicLong to the expected value 123 and if they are equal the new value of the AtomicLong becomes 234 ;
Adding to the AtomicLong Value
The AtomicLong class contains a few methods you can use to add a value to the AtomicLong and get its value returned. These methods are:
The first method, addAndGet() adds a number to the AtomicLong and returns its value after the addition. The second method, getAndAdd() also adds a number to the AtomicLong but returns the value the AtomicLong had before the value was added. Which of these two methods you should use depends on your use case. Here are two examples:
AtomicLong atomicLong = new AtomicLong(); System.out.println(atomicLong.getAndAdd(10)); System.out.println(atomicLong.addAndGet(10));
This example will print out the values 0 and 20 . First the example gets the value of the AtomicLong before adding 10 to. Its value before addition is 0. Then the example adds 10 to the AtomicLong and gets the value after the addition. The value is now 20.
You can also add negative numbers to the AtomicLong via these two methods. The result is effectively a subtraction.
The methods getAndIncrement() and incrementAndGet() works like getAndAdd() and addAndGet() but just add 1 to the value of the AtomicLong .
Subtracting From the AtomicLong Value
The AtomicLong class also contains a few methods for subtracting values from the AtomicLong value atomically. These methods are:
The decrementAndGet() subtracts 1 from the AtomicLong value and returns its value after the subtraction. The getAndDecrement() also subtracts 1 from the AtomicLong value but returns the value the AtomicLong had before the subtraction.
Using an AtomicLong as a Counter in a Lambda Expression
A Java Lambda Expression cannot contain any member fields, and thus they cannot keep any state internally between calls to the lambda expression. However, you can bypass this limitation by creating an AtomicLong outside the Lambda Expression and use it from inside the Lambda Expression. Here is an example of that:
import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; public class AtomicLongExample < public static void main(String[] args) < AtomicLong atomicLong = new AtomicLong(); FunctionmyLambda = (input) -> < long noOfCalls = atomicLong.incrementAndGet(); System.out.println("Lambda called " + noOfCalls + " times."); return input * 2; >; System.out.println(myLambda.apply(1L)); System.out.println(myLambda.apply(3L)); System.out.println(myLambda.apply(5L)); > >
The output from running the above code look like this:
Lambda called 1 times. 2 Lambda called 2 times. 6 Lambda called 3 times. 10