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:\n" + v1.get() + "\n" + (System.currentTimeMillis() - now));


        now = System.currentTimeMillis();
        executeThreads(new Runnable() {
            public void run() {
                testLock(v2);
            }
        });
        System.out.println("Lock:\n" + v2.getValue() + "\n" + (System.currentTimeMillis() - now));


        now = System.currentTimeMillis();
        executeThreads(new Runnable() {
            public void run() {
                testSynchronize(v3);
            }
        });
        System.out
            .println("Synchronized:\n" + v3.getValue() + "\n" + (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

posted @ 2016-06-22 13:21  uwannerme  阅读(503)  评论(1编辑  收藏  举报