zoukankan      html  css  js  c++  java
  • ReetrantLock Synchronized Atomic的性能对比

    之前看到了一篇帖子关于Lock和Synchronized的性能,写的是Lock比Synchronized的性能要好,可是,我试了下,结果却不是这样的,我所使用的JDK的版本是1.7,可能跟原帖作者用的JDK版本不一样,JDK对Synchronized做了优化。

    下面是我测试性能的代码:

    package juc;
    
    
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    /**
     * 测试Lock Synchronized Atomic的性能
     * Lock来改变变量,则必须用volatile修释,Atomic和Sychronized则保证了原子性和可见性
     * 耗时:Synchronized < ReetrantLock < Atomic
     * 可能JDK 1.7之后对Synchronized做了比较大的优化,以至于效率最高
     * @author jiujie
     * @version $Id: TestSpeed.java, v 0.1 2016年6月22日 上午10:24:42 jiujie Exp $
     */
    public class TestSpeed {
    
    
        private static final int LOOP_SIZE      = 100000;
    
    
        private static final int MAX_THREAD_NUM = 30;
    
    
        public static class IntBox {
    
    
            private Lock lock = new ReentrantLock();
    
    
            private int  value;
    
    
            public IntBox(int value) {
                this.value = value;
            }
    
    
            public int getValue() {
                return value;
            }
    
    
            public synchronized void sychronizedIncrease() {
                value++;
            }
    
    
            public void lockIncrease() {
                lock.lock();
                try {
                    value++;
                } finally {
                    lock.unlock();
                }
            }
    
    
        }
    
    
        private static void executeThreads(Runnable runnable) throws InterruptedException {
            for (int threadNum = 0; threadNum < MAX_THREAD_NUM; threadNum++) {
                Thread[] threads = new Thread[threadNum];
                for (int i = 0; i < threadNum; i++) {
                    threads[i] = new Thread(runnable);
                }
                for (Thread thread : threads) {
                    thread.start();
                }
                for (Thread thread : threads) {
                    thread.join();
                }
            }
        }
    
    
        public static void main(String[] args) throws InterruptedException {
    
    
            final AtomicInteger v1 = new AtomicInteger(0);
    
    
            final IntBox v2 = new IntBox(0);
    
    
            final IntBox v3 = new IntBox(0);
    
    
            long now = System.currentTimeMillis();
            executeThreads(new Runnable() {
                public void run() {
                    testAtomicLock(v1);
                }
            });
            System.out.println("Atomic:
    " + v1.get() + "
    " + (System.currentTimeMillis() - now));
    
    
            now = System.currentTimeMillis();
            executeThreads(new Runnable() {
                public void run() {
                    testLock(v2);
                }
            });
            System.out.println("Lock:
    " + v2.getValue() + "
    " + (System.currentTimeMillis() - now));
    
    
            now = System.currentTimeMillis();
            executeThreads(new Runnable() {
                public void run() {
                    testSynchronize(v3);
                }
            });
            System.out
                .println("Synchronized:
    " + v3.getValue() + "
    " + (System.currentTimeMillis() - now));
    
    
        }
    
    
        public static void testSynchronize(final IntBox v) {
            for (int i = 0; i < LOOP_SIZE; i++) {
                v.sychronizedIncrease();
            }
        }
    
    
        public static void testLock(final IntBox v) {
            for (int i = 0; i < LOOP_SIZE; i++) {
                v.lockIncrease();
            }
        }
    
    
        public static void testAtomicLock(final AtomicInteger v) {
            for (int i = 0; i < LOOP_SIZE; i++) {
                v.getAndIncrement();
            }
        }
    
    
    }
    

    运行结果如下:

    Atomic:
    43500000
    2998
    Lock:
    43500000
    1541
    Synchronized:
    43500000
    529

  • 相关阅读:
    Swagger2 添加HTTP head参数
    获取枚举类型描述
    JS设置cookie、读取cookie、删除cookie
    ES6中Promise的入门(结合例子)
    阮一峰的ES6---Promise对象
    model_util.py
    IfcSpatialElementType
    labelme coco
    python opencv KeyPoint
    IfcSpatialZoneType
  • 原文地址:https://www.cnblogs.com/uwannerme/p/5692827.html
Copyright © 2011-2022 走看看