Java源码如下:
- /**
- * Date formats are not synchronized.
- * It is recommended to create separate format instances for each thread.
- * If multiple threads access a format concurrently, it must be synchronized
- * externally.
- */
- public class SimpleDateFormat extends DateFormat {
- public Date parse(String text, ParsePosition pos){
- calendar.clear(); // Clears all the time fields
- // other logic ...
- Date parsedDate = calendar.getTime();
- }
- }
- abstract class DateFormat{
- // other logic ...
- protected Calendar calendar;
- public Date parse(String source) throws ParseException{
- ParsePosition pos = new ParsePosition(0);
- Date result = parse(source, pos);
- if (pos.index == 0)
- throw new ParseException("Unparseable date: \"" + source + "\"" ,
- pos.errorIndex);
- return result;
- }
- }
假定线程A和线程B都进入了parse(text, pos) 方法, 线程B执行到calendar.clear()后,线程A执行到calendar.getTime(), 那么就会有问题。
二. 问题重现:
- public class DateFormatTest {
- private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
- private static String date[] = { "01-Jan-1999", "01-Jan-2000", "01-Jan-2001" };
- public static void main(String[] args) {
- for (int i = 0; i < date.length; i++) {
- final int temp = i;
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- while (true) {
- String str1 = date[temp];
- String str2 = sdf.format(sdf.parse(str1));
- System.out.println(Thread.currentThread().getName() + ", " + str1 + "," + str2);
- if(!str1.equals(str2)){
- throw new RuntimeException(Thread.currentThread().getName()
- + ", Expected " + str1 + " but got " + str2);
- }
- }
- } catch (Exception e) {
- throw new RuntimeException("parse failed", e);
- }
- }
- }).start();
- }
- }
- }
创建三个进程, 使用静态成员变量SimpleDateFormat的parse和format方法,然后比较经过这两个方法折腾后的值是否相等:
程序如果出现以下错误,说明传入的日期就串掉了,即SimpleDateFormat不是线程安全的:
- Exception in thread "Thread-0" java.lang.RuntimeException: parse failed
- at cn.test.DateFormatTest$1.run(DateFormatTest.java:27)
- at java.lang.Thread.run(Thread.java:662)
- Caused by: java.lang.RuntimeException: Thread-0, Expected 01-Jan-1999 but got 01-Jan-2000
- at cn.test.DateFormatTest$1.run(DateFormatTest.java:22)
- ... 1 more
三. 解决方案:
1. 解决方案a:
将SimpleDateFormat定义成局部变量:
- SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
- String str1 = "01-Jan-2010";
- String str2 = sdf.format(sdf.parse(str1));
2. 解决方案b:
加一把线程同步锁:synchronized(lock)
- public class SyncDateFormatTest {
- private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
- private static String date[] = { "01-Jan-1999", "01-Jan-2000", "01-Jan-2001" };
- public static void main(String[] args) {
- for (int i = 0; i < date.length; i++) {
- final int temp = i;
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- while (true) {
- synchronized (sdf) {
- String str1 = date[temp];
- Date date = sdf.parse(str1);
- String str2 = sdf.format(date);
- System.out.println(Thread.currentThread().getName() + ", " + str1 + "," + str2);
- if(!str1.equals(str2)){
- throw new RuntimeException(Thread.currentThread().getName()
- + ", Expected " + str1 + " but got " + str2);
- }
- }
- }
- } catch (Exception e) {
- throw new RuntimeException("parse failed", e);
- }
- }
- }).start();
- }
- }
- }
3. 解决方案c: (推荐)
使用ThreadLocal: 每个线程都将拥有自己的SimpleDateFormat对象副本。
写一个工具类:
- public class DateUtil {
- private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
- public static Date parse(String str) throws Exception {
- SimpleDateFormat sdf = local.get();
- if (sdf == null) {
- sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
- local.set(sdf);
- }
- return sdf.parse(str);
- }
- public static String format(Date date) throws Exception {
- SimpleDateFormat sdf = local.get();
- if (sdf == null) {
- sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
- local.set(sdf);
- }
- return sdf.format(date);
- }
- }
- public class ThreadLocalDateFormatTest {
- private static String date[] = { "01-Jan-1999", "01-Jan-2000", "01-Jan-2001" };
- public static void main(String[] args) {
- for (int i = 0; i < date.length; i++) {
- final int temp = i;
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- while (true) {
- String str1 = date[temp];
- Date date = DateUtil.parse(str1);
- String str2 = DateUtil.format(date);
- System.out.println(str1 + "," + str2);
- if(!str1.equals(str2)){
- throw new RuntimeException(Thread.currentThread().getName()
- + ", Expected " + str1 + " but got " + str2);
- }
- }
- } catch (Exception e) {
- throw new RuntimeException("parse failed", e);
- }
- }
- }).start();
- }
- }
- }