标签:Java DateFormat SimpleDateFormat 日期 线程 compiledPattern time calendar
1.Date类为可变的,在多线程并发环境中会有线程安全问题。
(1)可以使用锁来处理并发问题。
(2)使用JDK8 Instant 或 LocalDateTime替代。
2.Calendar的子类为可变的,在多线程并发环境中会有线程安全问题。
(1)可以使用锁来处理并发问题。
(2)使用JDK8 LocalDateTime 替代。
3.DateFormat和SimpleDateFormat不是线程安全的原因
(1)DateFormat中calendar是共享变量,其子类SimpleDateFormat中也是共享变量。
DateFormat源码:
public abstract class DateFormat extends Format {
/**
* The {@link Calendar} instance used for calculating the date-time fields
* and the instant of time. This field is used for both formatting and
* parsing.
*
* <p>Subclasses should initialize this field to a {@link Calendar}
* appropriate for the {@link Locale} associated with this
* <code>DateFormat</code>.
* @serial
*/
protected Calendar calendar;
(2)SimpleDateFormat format方法源码:
private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) { // Convert input date to time field list calendar.setTime(date); boolean useDateFormatSymbols = useDateFormatSymbols(); for (int i = 0; i < compiledPattern.length; ) { int tag = compiledPattern[i] >>> 8; int count = compiledPattern[i++] & 0xff; if (count == 255) { count = compiledPattern[i++] << 16; count |= compiledPattern[i++]; } switch (tag) { case TAG_QUOTE_ASCII_CHAR: toAppendTo.append((char)count); break; case TAG_QUOTE_CHARS: toAppendTo.append(compiledPattern, i, count); i += count; break; default: subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols); break; } } return toAppendTo; }
当多个线程同时使用相同的 SimpleDateFormat 对象【如用static修饰的 SimpleDateFormat 】调用format方法时,多个线程会同时调用 calendar.setTime 方法,可能一个线程刚设置好 time 值另外的一个线程马上把设置的 time 值给修改了导致返回的格式化时间可能是错误的。
4.SimpleDateFormat线程安全使用。
(1)使用ThreadLocal处理static方法
public static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } };
System.out.println(df.get().format(new Date()));
2019-12-14
(2)使用JDK8 DateTimeFormatter 替代。
参考:https://www.cnblogs.com/wupeixuan/p/11511915.html?utm_source=gold_browser_extension
《阿里巴巴Java开发手册》
寻找撬动地球的支点(解决问题的方案),杠杆(Java等编程语言)已经有了。xkzhangsan
标签:Java,DateFormat,SimpleDateFormat,日期,线程,compiledPattern,time,calendar 来源: https://www.cnblogs.com/makai/p/15494123.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。