SimpleDateFormat线程不安全
现状
项目中出现一个现象,在格式化时间的时候出现ArrayIndexOutofBoundsException错误。
然后发现SimpleDateFormat是线程不安全的,除非是作为局部变量,被回收掉。
但是项目中是作为一个Util类的全局变量,而且是static的,导致多个servlet共用一个simpleDateFormat。
参考
https://blog.csdn.net/csdn_ds/article/details/72984646
参考如上代码然后进行试验,果然会出现数组越界异常,
public class SimpleDateFormateTest2 {
static SafeSimpleDateFormat safeSdf = new SafeSimpleDateFormat("yyyyMMdd,HHmmss");
public static void main(String[] args) {
final DateFormat df = new SimpleDateFormat("yyyyMMdd,HHmmss");
ExecutorService ts = Executors.newFixedThreadPool(100);
for (int i=0;i<10000;i++) {
ts.execute(new Runnable() {
@Override
public void run() {
try {
//生成随机数,格式化日期
String format = safeSdf.format(new Date(Math.abs(new Random().nextLong())));
System.out.println(format);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
});
}
}
}
然后根据试验了下parse方法,在100次循环内出现了异常,异常有很多展示。
试验代码如下
import com.fable.common.util.SafeSimpleDateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author lw
* @date 2019/10/15 0015
* @description
*/
public class SimpleDateFormateTest extends Thread {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SafeSimpleDateFormat safeSdf = new SafeSimpleDateFormat("yyyy-MM-dd");
private String name;
private String dateStr;
public SimpleDateFormateTest(String name, String dateStr) {
this.name = name;
this.dateStr = dateStr;
}
@Override
public void run() {
try {
Date date = sdf.parse(dateStr);
System.out.println(name + ": date:" + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void test() {
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.execute(new SimpleDateFormateTest("A", "2017-06-10"));
executorService.execute(new SimpleDateFormateTest("B", "2016-06-06"));
executorService.shutdown();
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++)
SimpleDateFormateTest.test();
}
}
解决
此处使用网页中推荐的第四种方案,写ThreadLocal.
import org.springframework.util.Assert;
import java.text.DateFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
/**
* This class implements a Thread-Safe (re-entrant) SimpleDateFormat
* class. It does this by using a ThreadLocal that holds a Map, instead
* of the traditional approach to hold the SimpleDateFormat in a ThreadLocal.
* <p>
* Each ThreadLocal holds a single HashMap containing SimpleDateFormats, keyed
* by a String format (e.g. "yyyy/M/d", etc.), for each new SimpleDateFormat
* instance that was created within the threads execution context.
*
* @author John DeRegnaucourt (jdereg@gmail.com)
* <br/>
* Copyright (c) John DeRegnaucourt
* <br/><br/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br/><br/>
* http://www.apache.org/licenses/LICENSE-2.0
* <br/><br/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class SafeSimpleDateFormat {
private final String _format;
private static final ThreadLocal<Map<String, SimpleDateFormat>> _dateFormats = new ThreadLocal<Map<String, SimpleDateFormat>>() {
public Map<String, SimpleDateFormat> initialValue() {
return new HashMap<String, SimpleDateFormat>();
}
};
private SimpleDateFormat getDateFormat(String format) {
Map<String, SimpleDateFormat> formatters = _dateFormats.get();
SimpleDateFormat formatter = formatters.get(format);
if (formatter == null) {
formatter = new SimpleDateFormat(format);
formatters.put(format, formatter);
}
return formatter;
}
public SafeSimpleDateFormat(String format) {
Assert.notNull(format);
_format = format;
}
public String format(Date date) {
return getDateFormat(_format).format(date);
}
public String format(Object date) {
return getDateFormat(_format).format(date);
}
public Date parse(String day) throws ParseException {
return getDateFormat(_format).parse(day);
}
public void setTimeZone(TimeZone tz) {
getDateFormat(_format).setTimeZone(tz);
}
public void setCalendar(Calendar cal) {
getDateFormat(_format).setCalendar(cal);
}
public void setNumberFormat(NumberFormat format) {
getDateFormat(_format).setNumberFormat(format);
}
public void setLenient(boolean lenient) {
getDateFormat(_format).setLenient(lenient);
}
public void setDateFormatSymbols(DateFormatSymbols symbols) {
getDateFormat(_format).setDateFormatSymbols(symbols);
}
public void set2DigitYearStart(Date date) {
getDateFormat(_format).set2DigitYearStart(date);
}
}
分类:
注意事项
标签:
java
, SimpleDateFormat
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?