📜  Java并发-AtomicInteger类

📅  最后修改于: 2020-11-15 03:53:55             🧑  作者: Mango


java.util.concurrent.atomic.AtomicInteger类提供了对基础int值的操作,这些值可以原子方式进行读写,并且还包含高级原子操作。 AtomicInteger支持对基本int变量的原子操作。它具有get和set方法,它们的工作方式类似于对易失性变量的读写。也就是说,一个集合与该变量的任何后续get都具有事前发生的关系。原子compareAndSet方法还具有这些内存一致性功能。

AtomicInteger方法

以下是AtomicInteger类中可用的重要方法的列表。

Sr.No. Method & Description
1

public int addAndGet(int delta)

Atomically adds the given value to the current value.

2

public boolean compareAndSet(int expect, int update)

Atomically sets the value to the given updated value if the current value is same as the expected value.

3

public int 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 int get()

Gets the current value.

7

public int getAndAdd(int delta)

Atomiclly adds the given value to the current value.

8

public int getAndDecrement()

Atomically decrements by one the current value.

9

public int getAndIncrement()

Atomically increments by one the current value.

10

public int getAndSet(int newValue)

Atomically sets to the given value and returns the old value.

11

public int 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(int 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(int newValue)

Sets to the given value.

16

public String toString()

Returns the String representation of the current value.

17

public boolean weakCompareAndSet(int expect, int update)

Atomically sets the value to the given updated value if the current value is same as the expected value.

下面的TestThread程序显示了在基于线程的环境中计数器的不安全实现。

public class TestThread {

   static class Counter {
      private int c = 0;

      public void increment() {
         c++;
      }

      public int value() {
         return c;
      }
   }
   
   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());
   }  
}

根据计算机的速度和线程交错,这可能会产生以下结果。

输出

Final number (should be 1000): 1000

以下TestThread程序显示了在基于线程的环境中使用AtomicInteger进行计数器的安全实现。

import java.util.concurrent.atomic.AtomicInteger;

public class TestThread {

   static class Counter {
      private AtomicInteger c = new AtomicInteger(0);

      public void increment() {
         c.getAndIncrement();
      }

      public int 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());
   }
}

这将产生以下结果。

输出

Final number (should be 1000): 1000