SimpleDateFormat 是 Java 中一个常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题.

因为 DateFormat 和 SimpleDateFormat 类不都是线程安全的,在多线程环境下调用 format() 和 parse() 方法应该使用同步代码来避免问题,
或者使用ThreadLocal, 也是将共享变量变为独享,线程独享肯定能比方法独享在并发环境中能减少不少创建对象的开销。
如果对性能要求比较高的情况下,一般推荐使用这种方法。

 public class DateUtil {

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

     static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}; private static DateFormat dateFormat() {
// return sdf;
return threadLocal.get();
} public static Date parse(String dateStr) throws ParseException {
return dateFormat().parse(dateStr);
} public static String format(Date date) {
return dateFormat().format(date);
} public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} try {
System.out.println(this.getName() + ":" + DateUtil.parse("2013-05-24 06:02:20"));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}.start();
}
}

参考:

http://www.cnblogs.com/zemliu/p/3290585.html

http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html

SimpleDateFormat的线程安全问题与解决方案的更多相关文章

  1. 044 SimpleDateFormat的线程安全问题与解决方案

    这个问题,以前好像写过,不过现在这篇文章,有一个重现的过程,还是值得读一读的. URL:SimpleDateFormat的线程安全问题与解决方案

  2. SimpleDateFormat类的线程安全问题和解决方案

    摘要:我们就一起看下在高并发下SimpleDateFormat类为何会出现安全问题,以及如何解决SimpleDateFormat类的安全问题. 本文分享自华为云社区<SimpleDateForm ...

  3. SimpleDateFormat的线程安全问题

    做项目的时候查询的日期总是不对,花了很长时间才找到异常的根源,原来SimpleDateFormat是非线程安全的,当我把这个类放到多线程的环境下转换日期就会出现莫名奇妙的结果,这种异常找出来可真不容易 ...

  4. SimpleDateFormat 的线程安全问题与解决方式

    SimpleDateFormat 的线程安全问题 SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的详细类. 它同意格式化 (date -> text).语法分析 (te ...

  5. 关于 SimpleDateFormat 的非线程安全问题及其解决方案

    一直以来都是直接用SimpleDateFormat开发的,没想着考虑线程安全的问题,特记录下来(摘抄的): 1.问题: 先来看一段可能引起错误的代码: package test.date; impor ...

  6. 转:浅谈SimpleDateFormat的线程安全问题

    转自:https://blog.csdn.net/weixin_38810239/article/details/79941964 在实际项目中,我们经常需要将日期在String和Date之间做转化, ...

  7. 使用原子类或synchronized(没用Lock)解决阐述多线程所遇到线程安全问题和解决方案

    例子题目: 创建10个线程,每个线程执行10000次加1,输出总和 正常结果100000  但是如果出现线程不安全会低于100000 import java.util.concurrent.Count ...

  8. SimpleDateFormat非线程安全

    文章列表 1)SimpleDateFormat的线程安全问题与解决方案 2)深入理解Java:SimpleDateFormat安全的时间格式化

  9. SimpleDateFormat类的安全问题,这6个方案总有一个适合你

    摘要:你使用的SimpleDateFormat类还安全吗?为什么说SimpleDateFormat类不是线程安全的?带着问题从本文中寻求答案. 本文分享自华为云社区<[高并发]SimpleDat ...

随机推荐

  1. handler的使用

    2014-04-15 10:45:06 简单学习了handler的使用. 昨天下载的问题,在手机上正常,在平板上不正常. 怀疑是网络的问题. 一直获得的流为空 2014-04-15 18:10:59 ...

  2. Mysql和Oracle的一些语法区别

    作为一个有追求的程序猿,当然要不断的学习,巴拉巴拉巴拉...好了,贴一个网址给大家,哈哈 MySQL与Oracle 差异比较:http://www.cnblogs.com/HondaHsu/p/364 ...

  3. 一个a::before的写法

    #key_table table tr td a::before{//这是个a前面的蓝色小圆点    background: #48A7D9;    content: "";//这 ...

  4. Compilation err ororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatException

    严重: Compilation errororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatExceptionat org.eclipse.j ...

  5. webx--petstore

    配置对应环境,运行petstore 通过官网给的命令行方法,来运行petstore petstore是java ee的经典学习案例,下载链接 如何运行呢? 参见官网给的指导:webx官网 git cl ...

  6. 兼容ie6及以上的阴影滤镜的写法

    .subnav{ width: 220px; _width:160px; min-height: 168px; border: 1px solid #d0d8da; background: #fff; ...

  7. 负载均衡软件LVS分析四(测试)

    一.启动LVS集群服务LVS负载均衡管理和使用有两种方式,一种是以ipvsadm命令行脚步与ldirectord监控方式,一种是以Piranha工具进行管理和使用.下面分别介绍. 1.利用ipvsad ...

  8. 常用js类型相互转换

    数字转换为字符串 var a=200.21;document.write(a.toString(10));  结果为:200.21以十进制转换 document.write(a.toFixed(3)) ...

  9. 模拟java的split函数,分割字符串,类似于java的split方法

    /*自定义oracle的分割函数*//*定义一个type,用户接收返回的数据集合类型*/create or replace type splitType as table of varchar2(40 ...

  10. 自定义IHttpModule

    HttpModule作用是 IIS将接收到的请求分发给相应的ISAPI处理前,先截获该请求. 通过这个我们可以完成很多额外功能. 自定义IHttpModule的例子: 通过自定义HttpModule, ...