zoukankan      html  css  js  c++  java
  • java中关于AtomicInteger的使用

    在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。

    没上锁的 Integer ++ ,执行多几次,得到的数值大多数都是小于200000,但是这个是我们不想得到的结果

    class MyThread implements Runnable {
        static volatile long i = 0;
    
        public void run() {
            for (int m = 0; m < 100000; m++) {
                i++;
            }
        }
    };
    
    public class TestAtomicInteger {
        public static void main(String[] args) throws InterruptedException {
            MyThread mt = new MyThread();
    
            Thread t1 = new Thread(mt);
            Thread t2 = new Thread(mt);
            t1.start();
            t2.start();
            Thread.sleep(500);
            System.out.println(MyThread.i);
        }
    }

    可以不用synchronized,使用AtomicInteger完成这个操作

    class MyThread implements Runnable {
        //    static  int i = 0;
        static AtomicInteger ai = new AtomicInteger(0);
    
    
        public void run() {
            for (int m = 0; m < 1000000; m++) {
                ai.getAndIncrement();
            }
        }
    };
    
    public class TestAtomicInteger {
        public static void main(String[] args) throws InterruptedException {
            MyThread mt = new MyThread();
    
            Thread t1 = new Thread(mt);
            Thread t2 = new Thread(mt);
            t1.start();
            t2.start();
            Thread.sleep(500);
            System.out.println(MyThread.ai.get());
        }
    }

    可以发现结果都是2000000,也就是说AtomicInteger是线程安全的。

    atomicinteger源码部分讲解

    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类不要随意使用,要在使用场景下使用。

    /*
     该类主要提供了两个方法put()和take(),前者将一个对象放到队列中,如果队列已经满了,就等待直到有空闲节点;后者从head取一个对象,如果没有对象,就等待直到有可取的对象。
    下面的例子比较简单,一个读线程,用于将要处理的文件对象添加到阻塞队列中,另外四个写线程用于取出文件对象,为了模拟写操作耗时长的特点,特让线程睡眠一段随机长度的时间。
    另外,该Demo也使用到了线程池和原子整型(AtomicInteger),AtomicInteger可以在并发情况下达到原子化更新,避免使用了synchronized,而且性能非常高。
    由于阻塞队列的put和take操作会阻塞,为了使线程退出,特在队列中添加了一个“标识”,算法中也叫“哨兵”,当发现这个哨兵后,写线程就退出。
    当然线程池也要显式退出了。
     */
    public class TestAtomicInteger {
    
        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:\txt");
            // 完成标志
            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(".txt");
                            }
                        });
                        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();
        }
    
    }
  • 相关阅读:
    Effective Java 第三版——72. 赞成使用标准异常
    Effective Java 第三版——71. 避免不必要地使用检查异常
    Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常
    Effective Java 第三版——69. 仅在发生异常的条件下使用异常
    Effective Java 第三版——68. 遵守普遍接受的命名约定
    Effective Java 第三版——67. 明智谨慎地进行优化
    Effective Java 第三版——66. 明智谨慎地使用本地方法
    Effective Java 第三版——65. 接口优于反射
    Effective Java 第三版——64. 通过对象的接口引用对象
    Effective Java 第三版——63. 注意字符串连接的性能
  • 原文地址:https://www.cnblogs.com/tinya/p/8494804.html
Copyright © 2011-2022 走看看