java LongAdder 和 AtomicLong 累加 测试比较

package atomadder;

import java.text.MessageFormat;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.logging.Logger;

public class test {
	private static Logger logger = Logger.getLogger("LoggingDemo");
	public static void atomicLong() throws Throwable {
        final int threadCount = 32;
        final AtomicLong longValue = new AtomicLong();
        Thread[] threads = new Thread[threadCount];
        for(int i=0; i<threadCount; i++){
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0; i<100*10000; i++){
                        longValue.incrementAndGet();
                    }
                }
            });
        }
        long time = System.nanoTime();
        for(int i=0; i<threadCount; i++){
            threads[i].start();
        }
        for(int i=0; i<threadCount; i++){
            threads[i].join();
        }
       
        StringBuilder sb= new StringBuilder();
       
        
        //MessageFormat.format("我是{0},我来自{1},今年{2}岁", "中国人", "北京", "22");
        logger.info(String.format("value: %d, time:%d", longValue.get(), System.nanoTime() - time));
        logger.info(MessageFormat.format("value: {0}, time:{1}", longValue.get(), System.nanoTime() - time));
        //logger.info("value: {}, time: {}", longValue.get(), System.nanoTime() - time);
    }
	
    public static void longAdder() throws Throwable {
        final int threadCount = 32;
        final LongAdder longAdder = new LongAdder();
        Thread[] threads = new Thread[threadCount];
        for(int i=0; i<threadCount; i++){
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0; i<100*10000; i++){
                        longAdder.increment();
                    }
                }
            });
        }
        long time = System.nanoTime();
        for(int i=0; i<threadCount; i++){
            threads[i].start();
        }
        for(int i=0; i<threadCount; i++){
            threads[i].join();
        }
        logger.info(MessageFormat.format("value: {0}, time: {1}", longAdder.longValue(), System.nanoTime() - time));
    }

	  public static void main(String[] args) throws Throwable {
		  atomicLong();
		  logger.info("-----------------------");
		  longAdder();
	    }

}

结果:

十一月 29, 2019 10:12:14 下午 atomadder.test atomicLong
信息: value: 32000000, time:645934347
十一月 29, 2019 10:12:14 下午 atomadder.test atomicLong
信息: value: 32,000,000, time:704,913,473
十一月 29, 2019 10:12:14 下午 atomadder.test main
信息: -----------------------
十一月 29, 2019 10:12:15 下午 atomadder.test longAdder
信息: value: 32,000,000, time: 337,813,303
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值