Java中对AtomicInteger和int值在多线程下递增操作的测试

 更新时间:2014年09月19日 09:56:12   投稿:junjie  
这篇文章主要介绍了Java中对AtomicInteger和int值在多线程下递增操作的测试,本文得出AtomicInteger操作 与 int操作的效率大致相差在50-80倍上下的结论,需要的朋友可以参考下

Java针对多线程下的数值安全计数器设计了一些类,这些类叫做原子类,其中一部分如下:

java.util.concurrent.atomic.AtomicBoolean;
java.util.concurrent.atomic.AtomicInteger;
java.util.concurrent.atomic.AtomicLong;
java.util.concurrent.atomic.AtomicReference;

下面是一个对比  AtomicInteger 与 普通 int 值在多线程下的递增测试,使用的是 junit4;

完整代码:

package test.java;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * 测试AtomicInteger与普通int值在多线程下的递增操作
 */
public class TestAtomic {

 // 原子Integer递增对象
 public static AtomicInteger counter_integer;// = new AtomicInteger(0);
 // 一个int类型的变量
 public static int count_int = 0;

 @Before
 public void setUp() {
 // 所有测试开始之前执行初始设置工作
 counter_integer = new AtomicInteger(0);
 }

 @Test
 public void testAtomic() throws InterruptedException {
 // 创建的线程数量
 int threadCount = 100;
 // 其他附属线程内部循环多少次
 int loopCount = 10000600;
 // 控制附属线程的辅助对象;(其他await的线程先等着主线程喊开始)
 CountDownLatch latch_1 = new CountDownLatch(1);
 // 控制主线程的辅助对象;(主线程等着所有附属线程都运行完毕再继续)
 CountDownLatch latch_n = new CountDownLatch(threadCount);
 // 创建并启动其他附属线程
 for (int i = 0; i < threadCount; i++) {
  Thread thread = new AtomicIntegerThread(latch_1, latch_n, loopCount);
  thread.start();
 }
 long startNano = System.nanoTime();
 // 让其他等待的线程统一开始
 latch_1.countDown();
 // 等待其他线程执行完
 latch_n.await();
 //

 long endNano = System.nanoTime();
 int sum = counter_integer.get();
 //
 Assert.assertEquals("sum 不等于 threadCount * loopCount,测试失败",
  sum, threadCount * loopCount);
 System.out.println("--------testAtomic(); 预期两者相等------------");
 System.out.println("耗时: " + ((endNano - startNano) / (1000 * 1000)) + "ms");
 System.out.println("threadCount = " + (threadCount) + ";");
 System.out.println("loopCount = " + (loopCount) + ";");
 System.out.println("sum = " + (sum) + ";");
 }

 @Test
 public void testIntAdd() throws InterruptedException {
 // 创建的线程数量
 int threadCount = 100;
 // 其他附属线程内部循环多少次
 int loopCount = 10000600;
 // 控制附属线程的辅助对象;(其他await的线程先等着主线程喊开始)
 CountDownLatch latch_1 = new CountDownLatch(1);
 // 控制主线程的辅助对象;(主线程等着所有附属线程都运行完毕再继续)
 CountDownLatch latch_n = new CountDownLatch(threadCount);
 // 创建并启动其他附属线程
 for (int i = 0; i < threadCount; i++) {
  Thread thread = new IntegerThread(latch_1, latch_n, loopCount);
  thread.start();
 }
 long startNano = System.nanoTime();
 // 让其他等待的线程统一开始
 latch_1.countDown();
 // 等待其他线程执行完
 latch_n.await();
 //
 long endNano = System.nanoTime();
 int sum = count_int;
 //
 Assert.assertNotEquals(
  "sum 等于 threadCount * loopCount,testIntAdd()测试失败", 
  sum, threadCount * loopCount);
 System.out.println("-------testIntAdd(); 预期两者不相等---------");
 System.out.println("耗时: " + ((endNano - startNano) / (1000*1000))+ "ms");
 System.out.println("threadCount = " + (threadCount) + ";");
 System.out.println("loopCount = " + (loopCount) + ";");
 System.out.println("sum = " + (sum) + ";");
 }

 // 线程
 class AtomicIntegerThread extends Thread {
 private CountDownLatch latch = null;
 private CountDownLatch latchdown = null;
 private int loopCount;

 public AtomicIntegerThread(CountDownLatch latch,
  CountDownLatch latchdown, int loopCount) {
  this.latch = latch;
  this.latchdown = latchdown;
  this.loopCount = loopCount;
 }

 @Override
 public void run() {
  // 等待信号同步
  try {
  this.latch.await();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  //
  for (int i = 0; i < loopCount; i++) {
  counter_integer.getAndIncrement();
  }
  // 通知递减1次
  latchdown.countDown();
 }
 }

 // 线程
 class IntegerThread extends Thread {
 private CountDownLatch latch = null;
 private CountDownLatch latchdown = null;
 private int loopCount;

 public IntegerThread(CountDownLatch latch, 
  CountDownLatch latchdown, int loopCount) {
  this.latch = latch;
  this.latchdown = latchdown;
  this.loopCount = loopCount;
 }

 @Override
 public void run() {
  // 等待信号同步
  try {
  this.latch.await();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  //
  for (int i = 0; i < loopCount; i++) {
  count_int++;
  }
  // 通知递减1次
  latchdown.countDown();
 }
 }
}

普通PC机上的执行结果类似如下:

--------------testAtomic(); 预期两者相等-------------------
耗时: 85366ms
threadCount = 100;
loopCount = 10000600;
sum = 1000060000;
--------------testIntAdd(); 预期两者不相等-------------------
耗时: 1406ms
threadCount = 100;
loopCount = 10000600;
sum = 119428988;

从中可以看出, AtomicInteger操作 与 int操作的效率大致相差在50-80倍上下,当然,int很不消耗时间,这个对比只是提供一个参照。

如果确定是单线程执行,那应该使用 int; 而int在多线程下的操作执行的效率还是蛮高的, 10亿次只花了1.5秒钟;

 (假设CPU是 2GHZ,双核4线程,理论最大8GHZ,则每秒理论上有80亿个时钟周期,

 10亿次Java的int增加消耗了1.5秒,即 120亿次运算, 算下来每次循环消耗CPU周期 12个;

个人觉得效率不错, C 语言也应该需要4个以上的时钟周期(判断,执行内部代码,自增判断,跳转)

 前提是: JVM和CPU没有进行激进优化.

)

而 AtomicInteger 效率其实也不低,10亿次消耗了80秒, 那100万次大约也就是千分之一,80毫秒的样子.

相关文章

  • 使用Spring boot 的profile功能实现多环境配置自动切换

    使用Spring boot 的profile功能实现多环境配置自动切换

    这篇文章主要介绍了使用Spring boot 的profile功能实现多环境配置自动切换的相关知识,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2018-11-11
  • 如何为Logback日志添加唯一追踪ID

    如何为Logback日志添加唯一追踪ID

    本文介绍了如何为Logback日志添加唯一追踪ID,以便在测试和调试时更容易定位报错信息,通过创建过滤器和修改配置文件,可以在每个请求的日志中添加唯一的ID,并将其返回给前端,这样,当用户反馈报错时,开发人员可以根据ID快速定位和解决问题
    2024-12-12
  • SpringBoot4.5.2 整合HikariCP 数据库连接池操作

    SpringBoot4.5.2 整合HikariCP 数据库连接池操作

    这篇文章主要介绍了SpringBoot4.5.2 整合HikariCP 数据库连接池操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringBoot Admin健康检查功能的实现

    SpringBoot Admin健康检查功能的实现

    admin主要就是告诉运维人员,服务出现异常,然后进行通知(微信、邮件、短信、钉钉等)可以非常快速通知到运维人员,相当报警功能,接下来通过本文给大家介绍SpringBoot Admin健康检查的相关知识,一起看看吧
    2021-06-06
  • 浅谈spring中的default-lazy-init参数和lazy-init

    浅谈spring中的default-lazy-init参数和lazy-init

    下面小编就为大家带来一篇浅谈spring中的default-lazy-init参数和lazy-init。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Java字符串数字左右补全0的四种方式

    Java字符串数字左右补全0的四种方式

    这篇文章主要给大家介绍了关于Java字符串数字左右补全0的四种方式,在编程中有时候我们需要对一个字符串进行字符填充,以满足某些特定的要求,其中补全0是一种常见的需求,需要的朋友可以参考下
    2023-08-08
  • Java中的注解、元注解详细解析

    Java中的注解、元注解详细解析

    这篇文章主要介绍了Java中的注解、元注解详细解析,注解也叫元数据,与类、接口、枚举是在同一个层次,它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释,需要的朋友可以参考下
    2023-11-11
  • 详解spring cloud整合Swagger2构建RESTful服务的APIs

    详解spring cloud整合Swagger2构建RESTful服务的APIs

    这篇文章主要介绍了详解spring cloud整合Swagger2构建RESTful服务的APIs,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Java日常练习题,每天进步一点点(3)

    Java日常练习题,每天进步一点点(3)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-07-07
  • IDEA 2020.3.X 创建scala环境的详细教程

    IDEA 2020.3.X 创建scala环境的详细教程

    这篇文章主要介绍了IDEA 2020.3.X 创建scala环境的详细教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04

最新评论