多线程报错:java.lang.NumberFormatException: multiple points

SimpleDateFormat是非线程安全的,在多线程情况下会有问题,在每个线程下得各自new SimpleDateFormat()就可以了

实现有两种方法:

1.

public class DateTools {

    public static Date parse(String formatPattern,String dateString) throws ParseException{
return new SimpleDateFormat(formatPattern).parse(dateString);
} public static String format(String formatPattern,Date date){
System.out.println("format");
return new SimpleDateFormat(formatPattern).format(date);
}
}

2. ThreadLocal 每个线程都有自己的私有数据

package threadDemo.date;

import java.text.SimpleDateFormat;

public class DateTools {

    private static ThreadLocal<SimpleDateFormat> threadLocals = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getSimpleDateFormat(String datePattern){
SimpleDateFormat sdf = null;
sdf = threadLocals.get();
if(sdf ==null){
sdf = new SimpleDateFormat(datePattern);
threadLocals.set(sdf);
}
return sdf;
}
}

最新文章

  1. MediaElement.js对不同浏览器的支持
  2. 微信公众平台开发(十) 消息回复总结——用其xml模板
  3. POJ-2528 Mayor&#39;s posters (线段树区间更新+离散化)
  4. file_get_contents函数和curl函数不能用
  5. C# 之 未能映射路径
  6. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)
  7. 【MooTools】
  8. Courses(最大匹配)
  9. linux下Oracle11g RAC搭建(九)
  10. html_Dom
  11. 【.NET Core项目实战-统一认证平台】第七章 网关篇-自定义客户端限流
  12. TCP连接笔记
  13. 4.3 explain 之 type
  14. margin塌陷与BFC总结
  15. php 操作json的各种格式
  16. 初始Java
  17. Rime中州韵导入极点五笔词库(附:自制词库)
  18. js中用来操作数组的相关的方法
  19. 【代码笔记】iOS-自定义switch
  20. 百度地图JS API不能使用position:fixed

热门文章

  1. Codeforces Round #258 (Div. 2) D
  2. android framework navigationbar自定义
  3. 【JZOJ4857】Tourist Attractions(Bitset)
  4. Codeforces679C. Bear and Square Grid
  5. Evaluate Reverse Polish Notation(逆波兰式)
  6. Eclipse-Java代码规范和质量检查插件-阿里编码规约
  7. Java循环中删除一个列表元素
  8. Yarn 的工作流-创建一个新项目
  9. Ajax使用JSON数据格式
  10. 实时人工智能:微软发布Project Brainwave预览版 现场可编程门阵列(Field Programmable Gate Array,简称FPGA) 硬件设计可以迅速演进