ITeye博客

原子计量器类AtomicLong, AtomicInteger, AtomicReference

379427555 · 2011-04-03 18:53 · jvee · 编程语言 · Markdown

J2SE 5.0提供了一组atomic class来帮助我们简化同步处理。基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增、减、赋值(更新)操作. 比如对于++运算符AtomicInteger可以将它持有的integer 能够atomic 地递增。在需要访问两个或两个以上 atomic变量的程序代码(或者是对单一的atomic变量执行两个或两个以上的操作)通常都需要被synchronize以便两者的操作能够被当作是一个atomic的单元。

对array atomic变量来说,一次只有一个索引变量可以变动,并没有功能可以对整个array做atomic化的变动。

关于Atomic的几个方法
getAndSet() : 设置新值,返回旧值.
compareAndSet(expectedValue, newValue) : 如果当前值(current value)等于期待的值(expectedValue), 则原子地更新指定值为新值(newValue), 如果更新成功,返回true, 否则返回false, 换句话可以这样说: 将原子变量设置为新的值, 但是如果从我上次看到的这个变量之后到现在被其他线程修改了(和我期望看到的值不符), 那么更新失败

从effective java (2)中拿来的一个关于AtomicReference的一个例子:
Java代码

Java代码

  1. public class AtomicTest {
  2. private int x, y;
  3. private enum State {
  4. NEW, INITIALIZING, INITIALIZED
  5. };
  6. private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
  7. public AtomicTest() {
  8. }
  9. public AtomicTest(int x, int y) {
  10. initialize(x, y);
  11. }
  12. private void initialize(int x, int y) {
  13. if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
  14. throw new IllegalStateException("initialize is error");
  15. }
  16. this.x = x;
  17. this.y = y;
  18. init.set(State.INITIALIZED);
  19. }
  20. public int getX() {
  21. checkInit();
  22. return x;
  23. }
  24. public int getY() {
  25. checkInit();
  26. return y;
  27. }
  28. private void checkInit() {
  29. if (init.get() == State.INITIALIZED) {
  30. throw new IllegalStateException("uninitialized");
  31. }
  32. }
  33. }
  34. public class AtomicTest {
  35. private int x, y;
  36. private enum State {
  37. NEW, INITIALIZING, INITIALIZED
  38. };
  39. private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
  40. public AtomicTest() {
  41. }
  42. public AtomicTest(int x, int y) {
  43. initialize(x, y);
  44. }
  45. private void initialize(int x, int y) {
  46. if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
  47. throw new IllegalStateException("initialize is error");
  48. }
  49. this.x = x;
  50. this.y = y;
  51. init.set(State.INITIALIZED);
  52. }
  53. public int getX() {
  54. checkInit();
  55. return x;
  56. }
  57. public int getY() {
  58. checkInit();
  59. return y;
  60. }
  61. private void checkInit() {
  62. if (init.get() == State.INITIALIZED) {
  63. throw new IllegalStateException("uninitialized");
  64. }
  65. }
  66. }
public class AtomicTest {   
    private int x, y;   
  
    private enum State {   
        NEW, INITIALIZING, INITIALIZED   
    };   
  
    private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);   
       
    public AtomicTest() {   
    }   
       
    public AtomicTest(int x, int y) {   
        initialize(x, y);   
    }   
  
    private void initialize(int x, int y) {   
        if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {   
            throw new IllegalStateException("initialize is error");   
        }   
        this.x = x;   
        this.y = y;   
        init.set(State.INITIALIZED);   
    }   
  
    public int getX() {   
        checkInit();   
        return x;   
    }   
  
    public int getY() {   
        checkInit();   
        return y;   
    }   
       
    private void checkInit() {   
        if (init.get() == State.INITIALIZED) {   
            throw new IllegalStateException("uninitialized");   
        }   
    }   
       
}  

public class AtomicTest {
 private int x, y;

 private enum State {
  NEW, INITIALIZING, INITIALIZED
 };

 private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
 
 public AtomicTest() {
 }
 
 public AtomicTest(int x, int y) {
  initialize(x, y);
 }

 private void initialize(int x, int y) {
  if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
   throw new IllegalStateException("initialize is error");
  }
  this.x = x;
  this.y = y;
  init.set(State.INITIALIZED);
 }

 public int getX() {
  checkInit();
  return x;
 }

 public int getY() {
  checkInit();
  return y;
 }
 
 private void checkInit() {
  if (init.get() == State.INITIALIZED) {
   throw new IllegalStateException("uninitialized");
  }
 }
 
}

上面的例子比较容易懂, 不过貌似没什么价值, 而在实际的应用中, 我们一般采用下面的方式来使用atomic class:
Java代码

Java代码

  1. public class CounterTest {
  2. AtomicInteger counter = new AtomicInteger(0);
  3. public int count() {
  4. int result;
  5. boolean flag;
  6. do {
  7. result = counter.get();
  8. // 断点
  9. // 单线程下, compareAndSet返回永远为true,
  10. // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
  11. // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
  12. flag = counter.compareAndSet(result, result + 1);
  13. } while (!flag);
  14. return result;
  15. }
  16. public static void main(String[] args) {
  17. final CounterTest c = new CounterTest();
  18. new Thread() {
  19. @Override
  20. public void run() {
  21. c.count();
  22. }
  23. }.start();
  24. new Thread() {
  25. @Override
  26. public void run() {
  27. c.count();
  28. }
  29. }.start();
  30. new Thread() {
  31. @Override
  32. public void run() {
  33. c.count();
  34. }
  35. }.start();
  36. }
  37. }
  38. public class CounterTest {
  39. AtomicInteger counter = new AtomicInteger(0);
  40. public int count() {
  41. int result;
  42. boolean flag;
  43. do {
  44. result = counter.get();
  45. // 断点
  46. // 单线程下, compareAndSet返回永远为true,
  47. // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
  48. // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
  49. flag = counter.compareAndSet(result, result + 1);
  50. } while (!flag);
  51. return result;
  52. }
  53. public static void main(String[] args) {
  54. final CounterTest c = new CounterTest();
  55. new Thread() {
  56. @Override
  57. public void run() {
  58. c.count();
  59. }
  60. }.start();
  61. new Thread() {
  62. @Override
  63. public void run() {
  64. c.count();
  65. }
  66. }.start();
  67. new Thread() {
  68. @Override
  69. public void run() {
  70. c.count();
  71. }
  72. }.start();
  73. }
  74. }
public class CounterTest {   
    AtomicInteger counter = new AtomicInteger(0);   
  
    public int count() {   
        int result;   
        boolean flag;   
        do {   
            result = counter.get();   
            // 断点   
            // 单线程下, compareAndSet返回永远为true,   
            // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,   
            // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值   
            flag = counter.compareAndSet(result, result + 1);   
        } while (!flag);   
  
        return result;   
    }   
  
    public static void main(String[] args) {   
        final CounterTest c = new CounterTest();   
        new Thread() {   
            @Override  
            public void run() {   
                c.count();   
            }   
        }.start();   
  
        new Thread() {   
            @Override  
            public void run() {   
                c.count();   
            }   
        }.start();   
  
        new Thread() {   
            @Override  
            public void run() {   
                c.count();   
            }   
        }.start();   
    }   
}  

public class CounterTest {
 AtomicInteger counter = new AtomicInteger(0);

 public int count() {
  int result;
  boolean flag;
  do {
   result = counter.get();
   // 断点
   // 单线程下, compareAndSet返回永远为true,
   // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
   // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
   flag = counter.compareAndSet(result, result + 1);
  } while (!flag);

  return result;
 }

 public static void main(String[] args) {
  final CounterTest c = new CounterTest();
  new Thread() {
   @Override
   public void run() {
    c.count();
   }
  }.start();

  new Thread() {
   @Override
   public void run() {
    c.count();
   }
  }.start();

  new Thread() {
   @Override
   public void run() {
    c.count();
   }
  }.start();
 }
}

类似i++这样的"读-改-写"复合操作(在一个操作序列中, 后一个操作依赖前一次操作的结果), 在多线程并发处理的时候会出现问题, 因为可能一个线程修改了变量, 而另一个线程没有察觉到这样变化, 当使用原子变量之后, 则将一系列的复合操作合并为一个原子操作,从而避免这种问题, i++=>i.incrementAndGet()
原子变量只能保证对一个变量的操作是原子的, 如果有多个原子变量之间存在依赖的复合操作, 也不可能是安全的, 另外一种情况是要将更多的复合操作作为一个原子操作, 则需要使用synchronized将要作为原子操作的语句包围起来. 因为涉及到可变的共享变量(类实例成员变量)才会涉及到同步, 否则不必使用synchronized

评论(1)

  1. #1 楼 · 543089122 · 2011-09-29 17:08

    LZ这代码贴的。。。惨不忍睹

评论功能维护中,当前仅展示历史评论。