本文翻译自http://tutorials.jenkov.com/java-util-concurrent/atomiclong.html,人工翻译,仅供学习交流。
AtomicLong
AtomicLong类为您提供了一个Long变量,它可以原子地读写。它也包含像compareAndSet()这样的高级原子操作。AtomicLong类位于java.util.concurrent.atomic包中,因此完整的类名java.util.concurrent.atomic.AtomicLong。本文描述了在Java 8中找到的AtomicLong版本,但第一个版本是在Java5中添加的。AtomicLong设计背后的原因在我的Java并发教程中关于比较和交换的文中解释过。
创建AtomicLong
创建AtomicLong是这样的:
AtomicLong atomicLong = new AtomicLong();
这个例子创建了一个初始值为0的AtomicLong。如果你想创建一个带有初始值的AtomicLong,你可以这样做:
AtomicLong atomicLong = new AtomicLong(123);
这个例子将值123作为参数传递给AtomicLong构造函数,它将AtomicLong实例的初始值设置为123。
获取AtomicLong值
您可以通过get()方法获取AtomicLong实例的值。下面是AtomicLong.get()的例子:
AtomicLong atomicLong = new AtomicLong(123);
long theValue = atomicLong.get();
设置AtomicLong值
您可以通过set()方法设置AtomicLong实例的值。下面是AtomicLong.set()的示例:
AtomicLong atomicLong = new AtomicLong(123);
atomicLong.set(234);
本例创建了一个AtomicLong示例,初始值为123,然后在下一行将其值设为234。
比较并设置AtomicLong值
AtomicLong类也有一个原子的compareAndSet()方法。该方法将AtomicLong实例的当前值与期望值进行比较,如果两个值相等,则为AtomicLong实例设置一个新值。下面是AtomicLong.compareAndSet()的例子:
AtomicLong atomicLong = new AtomicLong(123);
long expectedValue = 123;
long newValue = 234;
atomicLong.compareAndSet(expectedValue, newValue);
这个例子首先创建了一个AtomicLong实例,初始值为123,然后,它将AtomicLong的值与期望值123进行比较,如果它们相等,则AtomicLong的新值变为234;
添加到AtomicLong值
AtomicLong类包含一些方法,可以使用这些方法向AtomicLong添加并获得它的值。方法如下
- addAndGet()
- getAndAdd()
- getAndIncrement()
- incrementAndGet()
addAndGet()向AtomicLong添加一个数字,并在添加后返回它的值。getAndAdd()也向AtomicLong添加一个数字,但返回AtomicLong之前的值。您应该使用这两种方法中的哪一种取决于您的用例。下面是举例
AtomicLong atomicLong = new AtomicLong();
System.out.println(atomicLong.getAndAdd(10));
System.out.println(atomicLong.addAndGet(10));
本例将输出值0和20。第一个,在将10添加到之前,该示例获取AtomicLong的值。它在加法前的值是0。第二个,该示例将10添加到AtomicLong,并在添加之后获取值。您还可以通过这两个方法向AtomicLong添加负数,结果实际上是一个减法。getAndIncrement()和incrementAndGet()的工作方式类似于getAndAdd()和addAndGet(),但只是AtomicLong的值加1。
AtomicLong值中减去
AtomicLong类还包含一些从AtomicLong值中减去值的方法,方法如下
- decrementAndGet()
- getAndDecrement()
decrementAndGet()从AtomicLong值中减去1,并在减去后返回它的值,getAndDecrement()也从AtomicLong值中减去1,返回AtomicLong在减法之前的值。
在Lambda表达式中使用AtomicLong作为计数器
Java Lambda表达式不能包含任何成员字段,因此,在调用lambda表达式之间,它们不能在内部保持任何状态。您可以通过在Lambda表达式之外创建一个AtomicLong来绕过这个限制并且从Lambda表达式内部使用它。这里有一个例子:
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
public class AtomicLongExample {
public static void main(String[] args) {
AtomicLong atomicLong = new AtomicLong();
Function<Long, Long> myLambda = (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));
}
}
运行上述代码的输出如下所示:
Lambda called 1 times.
2
Lambda called 2 times.
6
Lambda called 3 times.
10