SimpleDateFormat非线程安全
来源:互联网 发布:怎么找放单的淘宝商家 编辑:程序博客网 时间:2024/05/19 02:00
类SimpleDateFormat主要负责日期的装换和格式化,但在多线程的环境中,使用此类容易造成数据转换及处理的不准确,因为SimpleDateFormat类并不是线程安全的。
package me.mymilkbottles.Study11;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by Administrator on 2017/07/17 17:08. */public class SimpleDateFormatStudy { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); String[] strings = new String[]{"2017", "2016", "2014", "2012", "2010", "2017", "1997", "1990", "1999", "1998", "1875", "1987", "1991", "1993", "1994", "1777", "1853", "1879", "1888", "1889"}; for (String date : strings) { new MyThread(sdf, date).start(); } }}class MyThread extends Thread { private SimpleDateFormat sdf; private String date; public MyThread(SimpleDateFormat sdf, String date) { this.sdf = sdf; this.date = date; } @Override public void run() { Date parseDate = null; try { parseDate = sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } if (!sdf.format(parseDate).toString().equals(date)) { System.out.println(sdf.format(parseDate).toString() + " != " + date); } }}
这是因为单例的SimpleDateFormat是不安全的,所以我们可以考虑使用多例。
package me.mymilkbottles.Study11;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by Administrator on 2017/07/17 17:08. */public class SimpleDateFormatStudy { public static void main(String[] args) { String[] strings = new String[]{"2017", "2016", "2014", "2012", "2010", "2017", "1997", "1990", "1999", "1998", "1875", "1987", "1991", "1993", "1994", "1777", "1853", "1879", "1888", "1889"}; for (String date : strings) { new MyThread(date).start(); } }}class MyThread extends Thread { private String date; public MyThread(String date) { this.date = date; } @Override public void run() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Date parseDate = null; try { parseDate = sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } if (!sdf.format(parseDate).toString().equals(date)) { System.out.println(sdf.format(parseDate).toString() + " != " + date); } }}
这种方法会创建多个SimpleDateFormat对象,如果我们希望少创建对象,我们可以考虑使用ThreadLocal。
package me.mymilkbottles.Study11;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by Administrator on 2017/07/17 17:08. */public class SimpleDateFormatStudy { public static void main(String[] args) { String[] strings = new String[]{"2017", "2016", "2014", "2012", "2010", "2017", "1997", "1990", "1999", "1998", "1875", "1987", "1991", "1993", "1994", "1777", "1853", "1879", "1888", "1889"}; for (String date : strings) { Thread thread = new MyThread(date); thread.start(); } }}class MyThread extends Thread { public static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>(); private String date; public MyThread(String date) { this.date = date; } @Override public void run() { SimpleDateFormat sdf = threadLocal.get(); if (sdf == null) { sdf = new SimpleDateFormat("yyyy"); threadLocal.set(sdf); } Date parseDate = null; try { parseDate = sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } if (!sdf.format(parseDate).toString().equals(date)) { System.out.println(sdf.format(parseDate).toString() + " != " + date); } }}
阅读全文
1 0
- SimpleDateFormat非线程安全
- SimpleDateFormat非线程安全
- SimpleDateFormat 非线程安全
- SimpleDateFormat非线程安全
- SimpleDateFormat非线程安全
- SimpleDateFormat 解决非线程安全
- 非线程安全类SimpleDateFormat
- ThreadLocal 解决SimpleDateFormat非线程安全
- ThreadLocal 解决SimpleDateFormat非线程安全
- JDK中的SimpleDateFormat线程非安全
- 非线程安全类SimpleDateFormat的禁忌
- ThreadLocal 解决SimpleDateFormat非线程安全
- Java多线程编程7--SimpleDateFormat非线程安全处理
- SimpleDateFormat 转为线程安全
- 线程安全之SimpleDateFormat
- SimpleDateFormat线程安全
- java SimpleDateFormat线程安全
- 线程安全的 SimpleDateFormat
- python中 Jupter的使用 notebook
- 各大巨头纷纷出手 数据中心市场硝烟弥漫
- java解析json格式数据
- 算法系列——Linked List Cycle II
- Cookie的路径和域
- SimpleDateFormat非线程安全
- 私有网络(腾讯云)
- 【PAT】【Advanced Level】1022. Digital Library (30)
- 位运算
- 随机森林原理介绍
- mybatis(7)--使用mapper代理开发代替原始的dao开发
- h5页面转盘抽奖
- 在命令行中更新android-sdk
- 内部类详解