An int value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.

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

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


实例:
import java.util.concurrent.atomic.AtomicInteger;

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

CommonJmeterTest.java
public abstract class CommonJmeterTest extends AbstractJavaSamplerClient {
  private static final Logger LOGGER = Logger
      .getLogger(CommonJmeterTest.class);
  protected static Map<Integer, String> memberMap = new HashMap<Integer, String>();
  protected static AtomicInteger iterNum = new AtomicInteger();
  protected UserSymbolService userSymbolService;
  protected ChangeVAccountIdService changeVAccountIdService;

  public void setupTest(JavaSamplerContext context) {
    userSymbolService = NakaraClientServicesLocator.getUserSymbolService();
    changeVAccountIdService = NakaraClientServicesLocator.getChangeVAccountIdService();
    LOGGER.info("init setupTest..ok");
  }

  static {
    vaccontImport();
  }

  @SuppressWarnings("unchecked")
  public static void vaccontImport() {
    File f = new File("account4.txt");
    List<String> memberIds = null;
    try {
      memberIds = FileUtils.readLines(f, null);
    } catch (IOException e) {
      LOGGER.error("Init error", e);
    }
    for (int i = 0; i < memberIds.size(); i++) {
      memberMap.put(i, memberIds.get(i).trim());
      iterNum.getAndAdd(1);
    }
  }
}
代码中,使用AtomicInteger 类型的iterNum,来保证iterNum的加一操作的原子性。在性能测试的过程中,保障线程安全