一直以為SimpleDateFormat是線程安全的,所以平時考慮到減少不必要的新建對象總是建一個全局靜態的來使用。最近在使用的時候發現了多線程時間覆蓋的問題,才意識到SimpleDateFormat是線程非安全的,希望大家以后多注意吧。
接下來的內容轉自:https://www.cnblogs.com/yangyongjie/p/11017409.html
一、
線程不安全驗證:
/** * SimpleDateFormat線程安全測試 * 〈功能詳細描述〉 * * @author 17090889 * @see [相關類/方法](可選) * @since [產品/模塊版本] (可選) */ public class SimpleDateFormatTest { private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000), new MyThreadFactory("SimpleDateFormatTest")); public void test() { while (true) { poolExecutor.execute(new Runnable() { @Override public void run() { String dateString = simpleDateFormat.format(new Date()); try { Date parseDate = simpleDateFormat.parse(dateString); String dateString2 = simpleDateFormat.format(parseDate); System.out.println(dateString.equals(dateString2)); } catch (ParseException e) { e.printStackTrace(); } } }); } }
輸出:
true
false
true
true
false
出現了false,說明線程不安全
1、format方法
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) { pos.beginIndex = pos.endIndex = 0; return format(date, toAppendTo, pos.getFieldDelegate()); } // Called from Format after creating a FieldDelegate 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; }
protected Calendar calendar;
可以看到,多個線程之間共享變量calendar,並修改calendar。因此在多線程環境下,當多個線程同時使用相同的SimpleDateFormat對象(如static修飾)的話,如調用format方法時,多個線程會同時調用calender.setTime方法,導致time被別的線程修改,因此線程是不安全的。
此外,parse方法也是線程不安全的,parse方法實際調用的是CalenderBuilder的establish來進行解析,其方法中主要步驟不是原子操作。
解決方案:
1、將SimpleDateFormat定義成局部變量
2、 加一把線程同步鎖:synchronized(lock)
3、使用ThreadLocal,每個線程都擁有自己的SimpleDateFormat對象副本。如:
/** * SimpleDateFormat線程安全測試 * 〈功能詳細描述〉 * * @author 17090889 * @see [相關類/方法](可選) * @since [產品/模塊版本] (可選) */ public class SimpleDateFormatTest { private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; // private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000), new MyThreadFactory("SimpleDateFormatTest")); public void test() { while (true) { poolExecutor.execute(new Runnable() { @Override public void run() { SimpleDateFormat simpleDateFormat = THREAD_LOCAL.get(); if (simpleDateFormat == null) { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } String dateString = simpleDateFormat.format(new Date()); try { Date parseDate = simpleDateFormat.parse(dateString); String dateString2 = simpleDateFormat.format(parseDate); System.out.println(dateString.equals(dateString2)); } catch (ParseException e) { e.printStackTrace(); } finally { local.remove(); } } }); } } }
4、使用DateTimeFormatter代替SimpleDateFormat
DateTimeFormatter是線程安全的,默認提供了很多格式化方法,也可以通過ofPattern方法創建自定義格式化方法。
(1)格式化日期示例:
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); // 2019-11-20T15:04:29.017 DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); String strDate=localDateTime.format(dtf); System.out.println(strDate); // 2019/23/20 15:23:46
(2)解析日期
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDateTime localDateTime=LocalDateTime.parse("2019/11/20 15:23:46",dtf); System.out.println(localDateTime); // 2019-11-20T15:23:46
(3)unix time格式化示例
DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dtf.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(1598327981l), ZoneId.systemDefault())));
二、使用JDK8全新的日期和時間API
1、LocalDate、LocalTime、LocalDateTime
Date如果不格式化,打印出的日期可讀性極差,可使用LocalDateTime代替。
(1)LocalDateTime:獲取年月日時分秒等於LocalDate+LocalTime
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); // 2019-11-20T15:04:29.017 LocalDate localDate = localDateTime.toLocalDate(); LocalTime localTime = localDateTime.toLocalTime();
(2)LocalDate:獲取年月日
LocalDate localDate=LocalDate.now(); System.out.println(localDate); // 2019-11-20
(3)LocalTime:獲取時分秒
LocalTime localTime = LocalTime.now(); System.out.println(localTime); // 15:14:17.081 int hour = localTime.getHour(); int minute = localTime.getMinute(); int second = localTime.getSecond();
2、Instant