博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SimpleDateFormat 的线程安全问题
阅读量:4059 次
发布时间:2019-05-25

本文共 4280 字,大约阅读时间需要 14 分钟。

1、问题:

SimpleDateFormat 的非线程安全问题:

package testDate;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class DateTester {
static SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); static String testdata[] = { "01-Jan-1999", "14-Feb-2001", "31-Dec-2007" }; public static void main(String[] args) { Runnable r[] = new Runnable[testdata.length]; for (int i = 0; i < r.length; i++) { final int i2 = i; r[i] = new Runnable() { public void run() { try { for (int j = 0; j < 2000; j++) { String str = testdata[i2]; String str2 = null; /* synchronized(df) */{ Date d = df.parse(str); str2 = df.format(d); System.out.println("i: " + i2 + "\tj: " + j + "\tThreadID: " + Thread.currentThread().getId() + "\tThreadName: " + Thread.currentThread().getName() + "\t" + str + "\t" + str2); } if (!str.equals(str2)) { throw new RuntimeException("date conversion failed after " + j + " iterations. Expected " + str + " but got " + str2); } } } catch (ParseException e) { throw new RuntimeException("parse failed"); } } }; new Thread(r[i]).start(); } }}

异常:

i: 1    j: 0    ThreadID: 19    ThreadName: Thread-6    14-Feb-2001 14-Jan-1999i: 2    j: 0    ThreadID: 20    ThreadName: Thread-7    31-Dec-2007 14-Jan-1999i: 0    j: 0    ThreadID: 18    ThreadName: Thread-5    01-Jan-1999 14-Jan-1999Exception in thread "Thread-7" Exception in thread "Thread-5" Exception in thread "Thread-6" java.lang.RuntimeException: date conversion failed after 0 iterations. Expected 31-Dec-2007 but got 14-Jan-1999java.lang.RuntimeException: date conversion failed after 0 iterations. Expected 01-Jan-1999 but got 14-Jan-1999java.lang.RuntimeException: date conversion failed after 0 iterations. Expected 14-Feb-2001 but got 14-Jan-1999    at testDate.DateTester$1.run(DateTester.java:31)    at testDate.DateTester$1.run(DateTester.java:31)	at testDate.DateTester$1.run(DateTester.java:31)    at java.lang.Thread.run(Thread.java:785)    at java.lang.Thread.run(Thread.java:785)    at java.lang.Thread.run(Thread.java:785)

恩,原因你是知道了,这是由于 SimpleDateFormat 的非线程安全问题引起的,

我们现在简化下问题,错误的代码应该是这样的:

public class DateUtil { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); public String formatDate(Date input) {      return sdf.format(input); }}

这里写图片描述


2 解决方案

(1)使用局部变量:

import java.text.SimpleDateFormat;import java.util.Date;public class DateUtil {
private static final String SIMPLE_FORMAT = "dd/MM/yyyy"; public String formatDate(Date input) { if(input == null){ return null; } SimpleDateFormat sdf = new SimpleDateFormat(SIMPLE_FORMAT);//local variable return sdf.format(input); }}

这里写图片描述

(2)使用 ThreadLocal

这里每个线程将有它自己的 SimpleDateFormat 副本。

import java.text.SimpleDateFormat;import java.util.Date;public class DateUtil {
// anonymous inner class. Each thread will have its own copy of the SimpleDateFormat private final static ThreadLocal
tl = new ThreadLocal
() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat("dd/MM/yyyy"); } } public String formatDate(Date input) { if (input == null) { return null; } return tl.get().format(input); }}

PS:顺便聊聊这个 ThreadLocal:

ThreadLocal 按我的理解是一个Map容器,视作其key是当前线程,value就是我们想保证数据安全一致的某对象。从它的功能上来说,应该叫做 ThreadLocalVariable(线程局部变量)更合适些。

具体的含义与作用请参考如下两篇文摘:ThreadLocal 那点事儿

(3)同步代码块 synchronized(code)

或者使用装饰器设计模式包装下 SimpleDateFormat ,使之变得线程安全。

(4)使用第三方的日期处理函数:

比如 JODA 来避免这些问题,你也可以使用 commons-lang 包中的 FastDateFormat 工具类。

(5)最后的提问:

上面几种方案中,有最佳方案吗?如果不是最佳,各有什么优劣?

参考 :

转载地址:http://lawji.baihongyu.com/

你可能感兴趣的文章
mongodb管理与安全认证
查看>>
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>
MongoDB 数据文件备份与恢复
查看>>
数据库索引介绍及使用
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>