首先看两段代码,一段是Integer的,一段是AtomicInteger的,为如下:java
public class Sample1 { private static Integer count = 0; synchronized public static void increment() { count++; } }
如下是AtomicInteger的:算法
public class Sample2 { private static AtomicInteger count = new AtomicInteger(0); public static void increment() { count.getAndIncrement(); } }
以上两段代码,在使用Integer的时候,必须加上synchronized保证不会出现并发线程同时访问的状况,而在AtomicInteger中却不用加上synchronized,在这里AtomicInteger是提供原子操做的,下面就对这进行相应的介绍。安全
AtomicInteger是一个提供原子操做的Integer类,经过线程安全的方式操做加减。多线程
AtomicInteger提供原子操做来进行Integer的使用,所以十分适合高并发状况下的使用。并发
public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } private volatile int value;
以上为AtomicInteger中的部分源码,在这里说下其中的value,这里value使用了volatile关键字,volatile在这里能够作到的做用是使得多个线程能够共享变量,可是问题在于使用volatile将使得VM优化失去做用,致使效率较低,因此要在必要的时候使用,所以AtomicInteger类不要随意使用,要在使用场景下使用。dom
如下就是在多线程状况下,使用AtomicInteger的一个实例,这段代码是借用IT宅中的一段代码。高并发
public class AtomicTest { static long randomTime() { return (long) (Math.random() * 1000); } public static void main(String[] args) { // 阻塞队列,能容纳100个文件 final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100); // 线程池 final ExecutorService exec = Executors.newFixedThreadPool(5); final File root = new File("D:\\ISO"); // 完成标志 final File exitFile = new File(""); // 原子整型,读个数 // AtomicInteger能够在并发状况下达到原子化更新,避免使用了synchronized,并且性能很是高。 final AtomicInteger rc = new AtomicInteger(); // 原子整型,写个数 final AtomicInteger wc = new AtomicInteger(); // 读线程 Runnable read = new Runnable() { public void run() { scanFile(root); scanFile(exitFile); } public void scanFile(File file) { if (file.isDirectory()) { File[] files = file.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getPath().endsWith(".iso"); } }); for (File one : files) scanFile(one); } else { try { // 原子整型的incrementAndGet方法,以原子方式将当前值加 1,返回更新的值 int index = rc.incrementAndGet(); System.out.println("Read0: " + index + " " + file.getPath()); // 添加到阻塞队列中 queue.put(file); } catch (InterruptedException e) { } } } }; // submit方法提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。 exec.submit(read); // 四个写线程 for (int index = 0; index < 4; index++) { // write thread final int num = index; Runnable write = new Runnable() { String threadName = "Write" + num; public void run() { while (true) { try { Thread.sleep(randomTime()); // 原子整型的incrementAndGet方法,以原子方式将当前值加 1,返回更新的值 int index = wc.incrementAndGet(); // 获取并移除此队列的头部,在元素变得可用以前一直等待(若是有必要)。 File file = queue.take(); // 队列已经无对象 if (file == exitFile) { // 再次添加"标志",以让其余线程正常退出 queue.put(exitFile); break; } System.out.println(threadName + ": " + index + " " + file.getPath()); } catch (InterruptedException e) { } } } }; exec.submit(write); } exec.shutdown(); } }
AtomicInteger是在使用非阻塞算法实现并发控制,在一些高并发程序中很是适合,但并不能每一种场景都适合,不一样场景要使用使用不一样的数值类。性能