JAVA的synchronized关键字与concurrent并发包的性能比较。

来源:互联网 发布:淘宝运费险能赔多少 编辑:程序博客网 时间:2024/05/19 18:47

     private static AtomicInteger mySessionID= new AtomicInteger(-2);

    public static int getMySessionID() {
        return mySessionID.decrementAndGet();
    }
    private static int mySessionID= -2;
    public static synchronized int getMySessionID() {
        return mySessionID--;
    }
经过开了300个线程,每个线程做一万次以上循环,发现,竟然Atomic的原子类竟然平均高出synchronized差不多3倍以上,并且速度非常稳定,而synchronized表现不稳定,而且速度也慢。
原创粉丝点击