Java多线程(三)SimpleDateFormat
2024-10-21 09:57:11
多线程报错: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;
}
}
最新文章
- MediaElement.js对不同浏览器的支持
- 微信公众平台开发(十) 消息回复总结——用其xml模板
- POJ-2528 Mayor&#39;s posters (线段树区间更新+离散化)
- file_get_contents函数和curl函数不能用
- C# 之 未能映射路径
- jstack(查看线程)、jmap(查看内存)和jstat(性能分析)
- 【MooTools】
- Courses(最大匹配)
- linux下Oracle11g RAC搭建(九)
- html_Dom
- 【.NET Core项目实战-统一认证平台】第七章 网关篇-自定义客户端限流
- TCP连接笔记
- 4.3 explain 之 type
- margin塌陷与BFC总结
- php 操作json的各种格式
- 初始Java
- Rime中州韵导入极点五笔词库(附:自制词库)
- js中用来操作数组的相关的方法
- 【代码笔记】iOS-自定义switch
- 百度地图JS API不能使用position:fixed
热门文章
- Codeforces Round #258 (Div. 2) D
- android framework navigationbar自定义
- 【JZOJ4857】Tourist Attractions(Bitset)
- Codeforces679C. Bear and Square Grid
- Evaluate Reverse Polish Notation(逆波兰式)
- Eclipse-Java代码规范和质量检查插件-阿里编码规约
- Java循环中删除一个列表元素
- Yarn 的工作流-创建一个新项目
- Ajax使用JSON数据格式
- 实时人工智能:微软发布Project Brainwave预览版 现场可编程门阵列(Field Programmable Gate Array,简称FPGA) 硬件设计可以迅速演进