自学内容网 自学内容网

IDEA阅读Java源码 SimpleDateFormat

IDEA阅读Java源码 SimpleDateFormat

一、阅读的代码

package org.example.bcbd;

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

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        System.out.println(now);
        sdf.format(now);
    }
}

在这段代码中,我希望阅读 SimpleDateFormatformat 方法,来判断其是否是线程不安全的。

二、IDEA操作

2.1 标记断点

Tips: 在阅读的方法前打好断点

image-20240413222239817

2.2 启用Debug

image-20240413222504871

2.3 按键区分

Tips: 在这里面的 Debug 按键是有不同功能的

  • 1 Step Over会直接返回方法运行结束的结果
  • 2 Step Into会进入方法,但是仅限于 自定义方法第三方库方法 不会进入 JDK 方法
  • 3 Step Out会跳出方法,一般和 (Force) Step Into 一起使用
  • 4 More 会有更多操作

image-20240413222646171

2.4 强制进入方法

Tips: 因为这里面只有一个方法,所以我们可以选用 Force Step Into 强制进入阅读 JDK 方法

image-20240413223311271

2.5 进入指定方法

Tips: 点击 Force Step Into 进入之后,发现几个问题

  • 这是 DateFormat 类 ,而不是 SimpleDateFormat 类,考虑应该是进入了其父类方法中
  • 这里面返回了另外一个同名的 format 函数,但是签名不同
  • 在返回的 format 方法中,我们可以看到有多个方法被调用如 new StringBuffer(), DontCareFieldPosition.INSTANCE,``format(date, new StringBuffer(),DontCareFieldPosition.INSTANCE).toString() 这几个方法

image-20240413223547044

2.6 多方法进入指定方法

Tips: 我不关心 new StringBuffer() 是如何实现,我也不关心 DontCareFieldPosition.INSTANCE 实现,我只关心 format 方法实现,但是如果我再次使用 Force Step Into 会直接进入 StringBuffer 的实现,而不是我想要进入的 format 实现。

这个时候我们就可以使用 Smart Step Into

image-20240413224453231

这是两个可进入的方法,我们鼠标点击 format 进入方法。

image-20240413224540540

2.7 进入正确的方法

Tips:

  • 类名正确了 SimpleDateFormat 说明没找错类
  • @Override 注解说明重写了父类的 format 方法
  • 又返回了一个 新的 format 方法

image-20240413224823135

继续用 Smart Step Into 进入新的 Format 方法

image-20240413225252176

2.8 真正的方法体实现

此时我们正看到 SimpleDateFormat 中的 format 实现

image-20240413225351391

以下是 format 方法源码

    // 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;
    }

三、SimpleDateFormat源码解析

​ 因为calendar是共享变量,并且这个共享变量没有做线程安全控制。当多个线程同时使用相同的SimpleDateFormat对象【如用static修饰的SimpleDateFormat】调用format方法时,多个线程会同时调用calendar.setTime方法,可能一个线程刚设置好time值另外的一个线程马上把设置的time值给修改了导致返回的格式化时间可能是错误的。在多并发情况下使用SimpleDateFormat需格外注意 SimpleDateFormat除了format是线程不安全以外,parse方法也是线程不安全的。parse方法实际调用alb.establish(calendar).getTime()方法来解析,alb.establish(calendar)方法里主要完成了.

    1. 重置日期对象cal的属性值
    1. 使用calb中中属性设置cal
    1. 返回设置好的cal对象

但是这三步不是原子操作

多线程并发如何保证线程安全?

  • 避免线程之间共享一个SimpleDateFormat对象,每个线程使用时都创建一次SimpleDateFormat对象 => 创建和销毁对象的开销大
  • 对使用formatparse方法的地方进行加锁 => 线程阻塞性能差
  • 使用ThreadLocal保证每个线程最多只创建一次SimpleDateFormat对象 => 较好的方法

Date对时间处理比较麻烦,比如想获取某年、某月、某星期,以及n天以后的时间,如果用Date来处理的话真是太难了,你可能会说Date类不是有getYeargetMonth这些方法吗,获取年月日很Easy,但都被弃用了啊。

四、参考文章

  1. 亲,建议你使用LocalDateTime而不是Date哦 - 掘金 (juejin.cn)

原文地址:https://blog.csdn.net/weixin_62636014/article/details/137729854

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!