Java Concurrency 简明教程
Java Concurrency - AtomicLong Class
java.util.concurrent.atomic.AtomicLong 类在可以读取和写入原子的底层长值上提供操作,还包含高级原子操作。AtomicLong 支持对底层长变量的原子操作。它具有像读写 volatile 变量那样的 get 和 set 方法。也就是说,一个 set 与对同一变量的任何后续 get 都具有 happens-before 关系。atomic compareAndSet 方法还具有这些内存一致性特性。
A java.util.concurrent.atomic.AtomicLong class provides operations on underlying long value that can be read and written atomically, and also contains advanced atomic operations. AtomicLong supports atomic operations on underlying long variable. It have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any subsequent get on the same variable. The atomic compareAndSet method also has these memory consistency features.
AtomicLong Methods
以下是 AtomicLong 类中提供的最重要方法列表。
Following is the list of important methods available in the AtomicLong class.
Sr.No. |
Method & Description |
1 |
public long addAndGet(long delta) Atomically adds the given value to the current value. |
2 |
public boolean compareAndSet(long expect, long update) Atomically sets the value to the given updated value if the current value is same as the expected value. |
3 |
public long decrementAndGet() Atomically decrements by one the current value. |
4 |
public double doubleValue() Returns the value of the specified number as a double. |
5 |
public float floatValue() Returns the value of the specified number as a float. |
6 |
public long get() Gets the current value. |
7 |
public long getAndAdd(long delta) Atomiclly adds the given value to the current value. |
8 |
public long getAndDecrement() Atomically decrements by one the current value. |
9 |
public long getAndIncrement() Atomically increments by one the current value. |
10 |
public long getAndSet(long newValue) Atomically sets to the given value and returns the old value. |
11 |
public long incrementAndGet() Atomically increments by one the current value. |
12 |
public int intValue() Returns the value of the specified number as an int. |
13 |
public void lazySet(long newValue) Eventually sets to the given value. |
14 |
public long longValue() Returns the value of the specified number as a long. |
15 |
public void set(long newValue) Sets to the given value. |
16 |
public String toString() Returns the String representation of the current value. |
17 |
public boolean weakCompareAndSet(long expect, long update) Atomically sets the value to the given updated value if the current value is same as the expected value. |
Example
TestThread 程序展示了在基于线程的环境中使用 AtomicLong 来安全实现计数器的过程。
The following TestThread program shows a safe implementation of counter using AtomicLong in thread based environment.
import java.util.concurrent.atomic.AtomicLong;
public class TestThread {
static class Counter {
private AtomicLong c = new AtomicLong(0);
public void increment() {
c.getAndIncrement();
}
public long value() {
return c.get();
}
}
public static void main(final String[] arguments) throws InterruptedException {
final Counter counter = new Counter();
//1000 threads
for(int i = 0; i < 1000 ; i++) {
new Thread(new Runnable() {
public void run() {
counter.increment();
}
}).start();
}
Thread.sleep(6000);
System.out.println("Final number (should be 1000): " + counter.value());
}
}
这将产生以下结果。
This will produce the following result.