commons-lang的FastDateFormat是一个thread-safe的,对SimpleDateFormat的一个重新实现。

SimpleDateFormat为什么不是thread-safe的呢?看一下具体实现就知道了,其父类中定义了成员变量Calendar,每次格式化日期时都会先重置这个Calendar的时间:calendar.setTime(date),这样多个线程不就出问题了。
而FastDateFormat是thread-safe的,但从其源代码看解决方案也很直接,每次格式化时都new一个Calendar对象出来,每次咱都用新的不就行了。单从这方面看FastDateFormat还真没Fast多少,但FastDateFormat比SimpleDateFormat还是先进了一点,对于同一种格式化日期的pattern,FastDateFormat可以保证只有一个实例产生,实现了对pattern的管理。
FastDateFormat的这种方式还是会带来一些问题,比如从缓存中获取pattern时在多个线程间的同步问题。从commons-lang2.6的源代码中看到下面的方法:
public static synchronized FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
可见大并发下还是会有锁等待的(当时为什么没有使用Double-check方式呢?)。commons-lang3.3对这部分做了优化,使用了ConcurrentHashMap作为缓存。
下面是我做的一些测试,
测试程序:
Case1:使用FastDateFormat
    public static long currentSystemTimeMillis() {        FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMddHHmmss");return Long.parseLong(fdf.format(System.currentTimeMillis()));    }

Case2:直接使用Calendar
    public static long currentSystemTimeMillis() {        Calendar rightNow = Calendar.getInstance();        rightNow.setTime(new Date(System.currentTimeMillis()));int year = rightNow.get(Calendar.YEAR);int month = rightNow.get(Calendar.MONTH) + 1;int day = rightNow.get(Calendar.DAY_OF_MONTH);int hour = rightNow.get(Calendar.HOUR_OF_DAY);int minute = rightNow.get(Calendar.MINUTE);int second = rightNow.get(Calendar.SECOND);        String strDateTime =            year                + (month < 10 ? "0" + month : month + "")                + (day < 10 ? "0" + day : day + "")                + (hour < 10 ? "0" + hour : hour + "")                + (minute < 10 ? "0" + minute : minute + "")                + (second < 10 ? "0" + second : second + "");return Long.parseLong(strDateTime);    }

//测试主方法
    public static void testDateFormat() throws Exception {        System.out.println("Begin test of currentSystemTimeMillis()");        System.out.println("currentSystemTimeMillis:"+currentSystemTimeMillis());int tCnt = 50;        Thread[] threads = new Thread[tCnt];for (int i = 0; i < tCnt; i++) {            Runnable run = new Runnable() {public void run() {try {int runCounter = 0;for (long i = 0; i < 100000l; i++) {                            currentSystemTimeMillis();                            runCounter++;                        }                        System.out.println(Thread.currentThread().getName()                            + " finished. runCounter="                            + runCounter);                    } catch (Exception e) {                    }                }            };            threads[i] = new Thread(run, "Thread" + i);        }long start = System.currentTimeMillis();for (int i = 0; i < tCnt; i++) {            threads[i].start();        }for (int i = 0; i < tCnt; i++) {            threads[i].join();        }        System.out.println("Test ended cost:" + (System.currentTimeMillis() - start));    }

测试环境:
CPU: AMD Phenom II 4核 3.4GHz
RAM: 4GB
OS : WINDOWS 7
50个线程,每个线程调用10万次
测试结果(程序总共执行耗时):
JVM参数:-Xms512m -Xmx512m
FastDateFormat  : 5078ms
直接使用Calendar: 3947ms
JVM参数:-server -Xms512m -Xmx512m
FastDateFormat  : 2716ms
直接使用Calendar: 2285ms
可见在纯粹的速度上FastDateFormat要比直接使用Calendar要慢,但对于服务器程序来说,开启-server后,差距会缩小,由于是在windows上测试的,开不开-server只是和是否使用Parallel GC有关(开启后Parallel GC很好地利用了4核cpu的优势,减少了一部分GC时间)。又由于本次并发量很大,所以可以预见在实际应用中,对于服务器程序而言,使用FastDateFormat后,性能影响应该不是很大。

转载于:https://www.cnblogs.com/snowboyovo/archive/2012/02/28/2371200.html

commons-lang的FastDateFormat性能测试相关推荐

  1. Spark - Illegal pattern component: XXX 与org.apache.commons.lang3.time.FastDateFormat incompatible

    一.引言 使用 sparkSession 读文件时出现 java.lang.IllegalArgumentException: Illegal pattern component: XXX 报错,解决 ...

  2. org.apache.commons.lang.StringUtils的jar包是什么

    org.apache.commons.lang.StringUtils的jar包是什么 commons-lang-2.5.jar

  3. org.apache.commons.lang.StringUtils

    org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...

  4. org.springframework.uti包下的StringUtils的使用和org.apache.commons.lang包下StringUtils的使用

    一.org.springframework.util.StringUtils StringUtils常用方法 描述 boolean isEmpty(Object str) 判断字符串是否为空,如果为n ...

  5. guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...

    guava和commons 最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,&quo ...

  6. 使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在

    最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,"一种检查变量是否包含诸如 ...

  7. Apache Commons Lang StringUtils

    因此,认为最好谈论我喜欢的另一个Java库. 它已经存在了一段时间,也许不是最令人兴奋的库,但是它非常有用. 我可能每天都使用它. org.apache.commons.lang.StringUtil ...

  8. java commons lang 随机数_Apache Common-lang组件里随机数工具类RandomStringUtils的一个bug...

    现在本文也转到了我自己的博客上,地址:月城小馆 Apache Common组件是java开发中常用的工具,其中的common-lang包是java基本数据类型的处理工具,包括数字.字符串.日期时间等多 ...

  9. apache.commons.lang.StringUtils 使用心得

    apache.commons.lang.StringUtils 使用心得 转载于:https://www.cnblogs.com/qinglizlp/p/5549687.html

  10. apache commons lang架包介绍

    commons lang组件介绍和学习 介绍 Java语言开发时有一个隐患,那就是java支持null值,这就导致很多时候操作可能会出异常. 因此很多第三方组件都会提供安全null safe 操作(即 ...

最新文章

  1. Liferay 6.1 用maven 构建 service-builder的巨大BUG的修复
  2. 我只会python、怎么搞个软件出来-python---很多行代码做一个自动打开软件的程序...
  3. dede日期时间标签调用大全
  4. Docker Swarm:经济高效的容器调度
  5. L1-048. 矩阵A乘以B
  6. 随手看的一本书《java微服务》,测试成功了其中的第一个样例
  7. PyTorch的nn.Linear()详解
  8. springboot 物联网_Confluent Kafka,KSQL,Spring Boot和分布式SQL开发物联网实战
  9. 矩形嵌套(NYOJ-16)
  10. HTTP 协议中的 Transfer-Encoding
  11. oracle的表连接-内外连接
  12. 创维E900V22C、E900V22D_S905L3A_安卓9.0_精简卡刷通刷固件包
  13. Xamarin 开发Android应用简易教程
  14. SVPWM算法原理及详解
  15. Mac删除Python缓存文件
  16. MGRE环境下的OSPF (实验超详解)
  17. BLOB/TEXT column ‘sup_content‘ used in key specification without a key length
  18. 什么是options请求?为什么会有options请求?
  19. IE8不支持jquery , xmlhttp.open() IE8 拒绝访问
  20. 赶快入手12代酷睿,畅享性能飞升体验-Acer掠夺者战斧300笔记本评测

热门文章

  1. MySQL5.5加主键锁读问题【转】
  2. 使用Visual Studio 2010 一步一步创建Powershell Module 和 Cmdlet
  3. POJ-1664 放苹果 动态规划思想解组合数学
  4. 新一代网络模式Web 2.0火爆发展
  5. Microsoft .NET 框架资源基础 ---摘自:msdn
  6. dos命令查看oracle_home,Oracle - OraDb10g_home1配置方法+DOS命令
  7. java corepoolsize_理解ThreadPoolExecutor线程池的corePoolSize、maximumPoolSize和poolSize
  8. mysql 赋给用户权限 grant all privileges on
  9. 最大公约数最小公倍数
  10. 昆士兰科技大学计算机专业,昆士兰科技大学QUT计算机科学Computer Science专业排名第101-125位(2021年THE世界大学商科排名)...