SimpleDateFormat线程安全问题详解

本文深入探讨了Java中SimpleDateFormat类的线程安全问题,分析了其内部机制导致的多线程环境下可能出现的异常,并提供了三种解决方案:局部变量、同步方法及ThreadLocal应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                              SimpleDateFormat线程安全问题详解

         在平时的工作中,我们经常需要将日期在String和Date之间做转化,此时需要使用SimpleDateFormat类。使用SimpleDateFormat类的parse方法,可以将满足格式要求的字符串转换成Date对象;使用SimpleDateFormat类的format方法,可以将Date类型的对象转换成一定格式的字符串!但是有一点需要特别注意,SimpleDateFormat并非是线程安全的,也就是说在并发环境下,如果考虑不周使用SimpleDateFormat方法可以会出现线程安全方面的问题

一、线程安全问题

1.示例

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created on 2018/11/03.
 */
public class DateUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static  String formatDate(Date date)throws ParseException {
        return sdf.format(date);
    }

    public static Date parse(String strDate) throws ParseException{

        return sdf.parse(strDate);
    }
}
import java.text.ParseException;

/**
 * Created on 2018/11/03.
 */
public class DateUtilTest {
    public static class SimpleDateFormatThread extends Thread {

        private String dateStr;

        public SimpleDateFormatThread(String dateStr){
            this.dateStr = dateStr;
        }

        @Override
        public void run() {
                try {
                    System.out.println(this.getName()+":"+DateUtil.parse(dateStr));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
        }
    }
    
    public static void main(String[] args) {
        String dateStr = "2018-11-03 10:02:47";
        for(int i = 0; i < 5; i++){
            new SimpleDateFormatThread(dateStr).start();
        }

    }
}

2.运行结果:

Exception in thread "Thread-4" Exception in thread "Thread-2" Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:601)
    at java.lang.Long.parseLong(Long.java:631)
    at java.text.DigitList.getLong(DigitList.java:195)
    at java.text.DecimalFormat.parse(DecimalFormat.java:2084)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
    at java.text.DateFormat.parse(DateFormat.java:364)
    at javaStudy.DateUtil.parse(DateUtil.java:19)
    at javaStudy.DateUtilTest$SimpleDateFormatThread.run(DateUtilTest.java:21)
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:601)
    at java.lang.Long.parseLong(Long.java:631)
    at java.text.DigitList.getLong(DigitList.java:195)
    at java.text.DecimalFormat.parse(DecimalFormat.java:2084)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
    at java.text.DateFormat.parse(DateFormat.java:364)
    at javaStudy.DateUtil.parse(DateUtil.java:19)
    at javaStudy.DateUtilTest$SimpleDateFormatThread.run(DateUtilTest.java:21)
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:601)
    at java.lang.Long.parseLong(Long.java:631)
    at java.text.DigitList.getLong(DigitList.java:195)
    at java.text.DecimalFormat.parse(DecimalFormat.java:2084)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
    at java.text.DateFormat.parse(DateFormat.java:364)
    at javaStudy.DateUtil.parse(DateUtil.java:19)
    at javaStudy.DateUtilTest$SimpleDateFormatThread.run(DateUtilTest.java:21)
Thread-3:Fri Nov 03 01:19:07 CST 2220
Thread-1:Fri Mar 03 02:02:47 CST 2220

3.对运行结果进行分析

   从结果中我们可以看到:总共运行5个线程(除了主线程main之外),其中:Thread-4、Thread-2和Thread-0都出现NumberFormatException;Thread-3和Thread-1虽然没有出现异常,但是我们输入的是:2018-11-03 10:02:47,但是输出的却是2220年的时间

二、为什么会出现线程不安全

public class SimpleDateFormat extends DateFormat {

    // the official serial version ID which says cryptically
    // which version we're compatible with
    static final long serialVersionUID = 4774881970558875024L;

    // the internal serial version which says which version was written
    // - 0 (default) for version up to JDK 1.1.3
    // - 1 for version from JDK 1.1.4, which includes a new field
    static final int currentSerialVersion = 1;
public abstract class DateFormat extends Format {

    /**
     * The {@link Calendar} instance used for calculating the date-time fields
     * and the instant of time. This field is used for both formatting and
     * parsing.
     *
     * <p>Subclasses should initialize this field to a {@link Calendar}
     * appropriate for the {@link Locale} associated with this
     * <code>DateFormat</code>.
     * @serial
     */
    protected Calendar calendar;

   通过上面的代码可以看出:SimpleDateFormat继承了DateFormat,在DateFormat中定义了一个protected属性的 Calendar类的对象:calendar。在SimpleDateFormat转换日期是通过Calendar对象来操作的,且calendar这个成员变量既被用于format方法也被用于parse方法

1.parse方法为什么线程不安全

 parse()调用情况:

            1)先调用DateFormat对象的public Date parse(String source) throws ParseException

            2)DateFormat对象的parse方法调用SimpleDateFormat对象的public Date parse(String text, ParsePosition pos)

            3)SimpleDateFormat对象的parse方法调用 CalendarBuilder 对象的 Calendar establish(Calendar cal)

            4)在 establish()中,做了cal.clear();把calendar清空且没有设置新值。如果此时线程A将calendar清空且没有设置新值,线程B也进入parse方法用到了SimpleDateFormat对象中的calendar对象,此时就会产生线程安全问题!

  2.format为什么线程不安全

1)DateFormat类中

    public final String format(Date date)
    {
        return format(date, new StringBuffer(),
                      DontCareFieldPosition.INSTANCE).toString();
    }

2)SimpleDateFormat类中

public StringBuffer format(Date date, StringBuffer toAppendTo,
                               FieldPosition pos)
    {
        pos.beginIndex = pos.endIndex = 0;
        return format(date, toAppendTo, pos.getFieldDelegate());
    }

    // Called from Format after creating a FieldDelegate
    private StringBuffer format(Date date, StringBuffer toAppendTo,
                                FieldDelegate delegate) {
        // Convert input date to time field list
        calendar.setTime(date);

        boolean useDateFormatSymbols = useDateFormatSymbols();

        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
            int count = compiledPattern[i++] & 0xff;
            if (count == 255) {
                count = compiledPattern[i++] << 16;
                count |= compiledPattern[i++];
            }

            switch (tag) {
            case TAG_QUOTE_ASCII_CHAR:
                toAppendTo.append((char)count);
                break;

            case TAG_QUOTE_CHARS:
                toAppendTo.append(compiledPattern, i, count);
                i += count;
                break;

            default:
                subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
                break;
            }
        }
        return toAppendTo;
    }

calendar.setTime(date)这条语句改变了calendar。假设两个线程持有了同一个SimpleDateFormat的实例,分别调用format方法:
  线程1调用format方法,改变了calendar这个字段。
  中断来了。
  线程2开始执行,它也改变了calendar。
  又中断了。
  线程1回来了,此时,calendar已然不是它所设的值,而是走上了线程2设计的道路。如果多个线程同时争抢calendar对象,则会出现各种问题,时间不对,线程挂死等等。

3.总结

    从parse() 和format()可以看出:成员变量calendar在用到的时候直接用,且在用的时候改变了calendar的值,这样如果多线程情况下,就会出现线程安全问题。实际上可以:在最初调用的时候定义一个局部变量calendar,一路通过参数传递下去,所有问题都将迎刃而解。

三、解决方案

1.需要的时候创建局部变量

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil1 {

    public static  String formatDate(Date date)throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date parse(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(strDate);
    }
}

    在需要用到SimpleDateFormat 的地方新建一个实例,不管什么时候,将有线程安全问题的对象由共享变为局部私有都能避免多线程问题,不过也加重了创建对象的负担。在一般情况下,这样其实对性能影响并不是很明显的。

2.创建一个共享的SimpleDateFormat实例变量,但在使用的时候,需要对这个变量进行同步

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil2 {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static  String formatDate(Date date)throws ParseException {
        synchronized (sdf){
            return sdf.format(date);
        }
    }

    public static Date parse(String strDate) throws ParseException{
        synchronized (sdf){
            return sdf.parse(strDate);
        }
    }
}

      当线程较多时,当一个线程调用该方法时,其他想要调用此方法的线程就要block,多线程并发量大的时候会对性能有一定的影响。
3.使用ThreadLocal为每个线程都创建一个线程独享的SimpleDateFormat变量

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil3 {

    private static ThreadLocal<DateFormat> sdfThreadLocal =  new ThreadLocal<DateFormat>(){
            @Override
            public SimpleDateFormat initialValue(){
               return  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
    };

    public static  String formatDate(Date date)throws ParseException {
        return sdfThreadLocal.get().format(date);
    }

    public static Date parse(String strDate) throws ParseException{

        return sdfThreadLocal.get().parse(strDate);
    }
}

     使用ThreadLocal, 也是将共享变量变为独享,线程独享肯定能比方法独享在并发环境中能减少不少创建对象的开销。如果对性能要求比较高的情况下,一般推荐使用这种方法。

<think>好的,我现在需要回答用户关于Java线程安全问题。首先,我得理解什么是线程安全线程安全通常指在多线程环境下,程序能够正确处理共享资源,避免数据不一致的问题。那Java中哪些情况会导致线程不安全呢?比如多个线程同时修改同一个变量,或者操作非原子性的步骤。 接下来,我应该举几个例子来说明问题。比如,经典的计数器例子,如果多个线程同时增加一个计数器,没有同步的话结果可能不正确。这时候需要用synchronized关键字或者原子类来解决。 然后,我得想到Java中常见的线程安全解决方案。比如使用同步代码块、锁机制(如ReentrantLock)、原子变量(AtomicInteger等)、以及并发容器(如ConcurrentHashMap)。还有volatile关键字的作用,虽然它不能保证原子性,但能保证可见性。 另外,需要区分不同的类是否是线程安全的。比如,StringBuilder是非线程安全的,而StringBuffer是线程安全的,因为后者方法加了synchronized。类似地,ArrayList和Vector的区别。 可能还需要提到线程本地存储ThreadLocal,它可以让每个线程有自己的变量副本,避免共享。这在某些场景下很有用,比如SimpleDateFormat的非线程安全问题,可以用ThreadLocal来解决。 用户可能还想知道如何判断一个类是否线程安全,这时候需要查看文档或者源码,看是否有同步措施。另外,设计线程安全的类需要注意哪些原则,比如状态封装、不可变对象等。 最后,总结一下线程安全的重要性,特别是在高并发环境下,正确的同步机制能避免很多难以调试的问题。同时提醒用户注意性能开销,合理选择同步策略,避免过度同步导致性能下降。 需要确保回答结构清晰,分点说明问题、原因和解决方案,使用例子帮助理解,语言要简洁易懂。同时检查是否有术语错误,比如原子性、可见性、有序性等概念是否正确。可能还需要提到JMM(Java内存模型)的相关知识,解释为什么会出现线程安全问题。 嗯,还要注意用户提到的格式要求,数学公式用$...$,但Java代码里可能不太涉及数学公式,不过如果有的话要正确使用。比如在解释原子操作的时候,可能用类似$i++$这样的表达式,说明其非原子性,需要正确转义。 最后检查是否覆盖了用户可能关心的点,比如常见的线程不安全案例、解决方案、工具类等,确保回答全面且有条理。</think>### Java线程安全问题详解 #### 一、什么是线程安全? 当多个线程**同时访问共享资源**时,若程序能始终保证**数据一致性**和**逻辑正确性**,则称为线程安全。反之,若出现数据错误、逻辑异常等问题,则称为**线程不安全**。 --- #### 二、典型线程不安全场景 1. **非原子操作** ```java public class Counter { private int count = 0; public void increment() { count++; // 实际包含"读-改-写"三步操作 } } ``` - `count++` 是非原子操作,多线程并发时可能导致值丢失。 2. **可见性问题** 一个线程修改共享变量后,其他线程可能**无法立即看到最新值**(因CPU缓存机制)。 3. **指令重排序** JVM或处理器可能优化代码执行顺序,导致意外结果。 --- #### 三、解决方案与工具 1. **同步代码块 (`synchronized`)** ```java public synchronized void increment() { count++; } ``` - 通过互斥锁保证**原子性**和**可见性**。 2. **显式锁 (`ReentrantLock`)** ```java private Lock lock = new ReentrantLock(); public void increment() { lock.lock(); try { count++; } finally { lock.unlock(); } } ``` - 更灵活的控制,支持公平锁、可中断等特性。 3. **原子类 (`java.util.concurrent.atomic`)** ```java private AtomicInteger count = new AtomicInteger(0); public void increment() { count.incrementAndGet(); // CAS操作保证原子性 } ``` - 基于CAS(Compare-And-Swap)无锁算法,性能较高。 4. **volatile 关键字** ```java private volatile boolean flag = false; ``` - 仅保证**可见性**和**禁止指令重排序**,不保证原子性。 5. **线程安全容器** - `ConcurrentHashMap`(分段锁/CAS) - `CopyOnWriteArrayList`(写时复制) - 替代非线程安全的`HashMap`、`ArrayList`等。 --- #### 四、常见线程安全类对比 | 非线程安全类 | 线程安全替代方案 | 实现原理 | |-------------------|---------------------------|-------------------------| | `StringBuilder` | `StringBuffer` | 方法添加`synchronized` | | `SimpleDateFormat`| 使用`ThreadLocal`包装 | 线程隔离 | | `HashMap` | `ConcurrentHashMap` | 分段锁+CAS | | `ArrayList` | `CopyOnWriteArrayList` | 写时复制 | --- #### 五、设计线程安全类的基本原则 1. **封装状态**:隐藏共享数据,仅通过受控方法访问。 2. **不可变对象**:使用`final`修饰变量(如`String`)。 3. **明确依赖**:在文档中声明类的线程安全级别(如`@ThreadSafe`)。 --- #### 六、性能与权衡 - 过度同步会导致**上下文切换开销**,降低并发性能。 - 根据场景选择方案: - 低竞争:原子类 > 显式锁 > `synchronized` - 高竞争:分段锁(如`ConcurrentHashMap`) --- #### 七、调试工具 - `jstack`:分析线程堆栈 - `VisualVM`:监控线程状态 - 压力测试工具(如JMeter) 通过理解这些机制,开发者能有效避免竞态条件(Race Condition)、死锁等问题,构建高可靠性的并发程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值