Java常用的时间日期API

java8之前常用的 new Date() 来表示时间,再使用SimpleDateFormat来对日期时间进行格式化处理,但在多线程并发下却存在线程安全问题

//使用SimpleDateFormat在并发下存在线程安全问题
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date d1 = format.parse("2000/10/10 ");
Date d2 = format.parse("2021/12/28");
Thread thread01 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = format.format(d1);
        System.out.println(s);
    }
});
Thread thread02 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = format.format(d2);
        System.out.println(s);
    }
});
thread01.start();
thread02.start();

打印结果如下如下:

2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2000/10/28
2000/10/10
2000/10/10
2000/10/10

里面出现了一个不该出现的数据:2000/10/28
这便是使用SimpleDateFormat线程不安全导致
解决方法:使用 ThreadLocal类为每个线程提供自己的局部变量,解决多线程并发下的数据安全问题

ThreadLocal<DateFormat> safeSdf = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy/MM/dd"));
Date d1 = safeSdf.get().parse("2000/10/10 ");
Date d2 = safeSdf.get().parse("2021/12/28");
Thread thread01 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = safeSdf.get().format(d1);
        System.out.println(s);
    }
});
Thread thread02 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = safeSdf.get().format(d2);
        System.out.println(s);
    }
});
thread01.start();
thread02.start();

打印结果如下如下:

2000/10/10
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2021/12/28
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10
2000/10/10

解决了线程安全问题:

ThreadLocal的设计是,每个Thread维护一个ThreadLocalMap
这个ThreadLocalMap的key是ThreadLocal实例本身,value就是真正存储的值Object
(ThreadLocalMap不是Map,而是自己维护了一个Key,Value的Entry数组)
每个Thread都有一个ThreadLocal,每个ThreadLocal都有一个ThreadLocalMap
private static ThreadLocal safeSdf = ThreadLocal.withInitial(() -> new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”));
上述代码可以解决多线程并发下操作同一个SimpleDateFormat对象造成的线程安全问题
让每一个线程都有独立的SimpleDateFormat对象

ThreadLocal 线程每次使用完就调用其remove()方法,把其value强引用给移除,让其被GC回收,ThreadLocal防止内存泄漏


java8 开始使用LocalDateTme和DateTimeFormatter来解决线程安全的问题,不需要我们使用ThreadLocal来解决线程安全的问题了

LocalDate d1 = LocalDate.of(2000, 10, 10);
LocalDate d2 = LocalDate.of(2021, 12, 28);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
Thread thread01 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = formatter.format(d1);
        System.out.println(s);
    }
});
Thread thread02 = new Thread(() -> {
    for (int i = 0; i < 10; i++) {
        String s = formatter.format(d2);
        System.out.println(s);
    }
});
thread01.start();
thread02.start();

打印结果如下如下:

2021/12/28
2021/12/28
2021/12/28
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2021/12/28
2000/10/10
2000/10/10
2000/10/10
2000/10/10

JAVA8中,新的时间日期API帮我们解决了线程安全问题~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值