Synchronizing access to SimpleDateFormat

前端 未结 9 1439
名媛妹妹
名媛妹妹 2020-12-02 06:56

The javadoc for SimpleDateFormat states that SimpleDateFormat is not synchronized.

\"Date formats are not synchronized. It is recommended to create

相关标签:
9条回答
  • 2020-12-02 07:09

    The other option is Commons Lang FastDateFormat but you can only use it for date formatting and not parsing.

    Unlike Joda, it can function as a drop-in replacement for formatting. (Update: Since v3.3.2, FastDateFormat can produce a FastDateParser, which is a drop-in thread-safe replacement for SimpleDateFormat)

    0 讨论(0)
  • 2020-12-02 07:10

    I just implemented this with Option 3, but made a few code changes:

    • ThreadLocal should usually be static
    • Seems cleaner to override initialValue() rather than test if (get() == null)
    • You may want to set locale and time zone unless you really want the default settings (defaults are very error prone with Java)

      private static final ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>() {
          @Override
          protected SimpleDateFormat initialValue() {
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-hh", Locale.US);
              sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
              return sdf;
          }
      };
      public String formatDate(Date d) {
          return tl.get().format(d);
      }
      
    0 讨论(0)
  • 2020-12-02 07:13

    I would say, create a simple wrapper-class for SimpleDateFormat that synchronizes access to parse() and format() and can be used as a drop-in replacement. More foolproof than your option #2, less cumbersome than your option #3.

    Seems like making SimpleDateFormat unsynchronized was a poor design decision on the part of the Java API designers; I doubt anyone expects format() and parse() to need to be synchronized.

    0 讨论(0)
  • 2020-12-02 07:19
    1. Creating SimpleDateFormat is expensive. Don't use this unless it's done seldom.

    2. OK if you can live with a bit of blocking. Use if formatDate() is not used much.

    3. Fastest option IF you reuse threads (thread pool). Uses more memory than 2. and has higher startup overhead.

    For applications both 2. and 3. are viable options. Which is best for your case depends on your use case. Beware of premature optimization. Only do it if you believe this is an issue.

    For libraries that would be used by 3rd party I'd use option 3.

    0 讨论(0)
  • 2020-12-02 07:21

    Commons Lang 3.x now has FastDateParser as well as FastDateFormat. It is thread safe and faster than SimpleDateFormat. It also uses the same format/parse pattern specifications as SimpleDateFormat.

    0 讨论(0)
  • 2020-12-02 07:21

    Don't use SimpleDateFormat, use joda-time's DateTimeFormatter instead. It is a bit stricter in the parsing side and so isn't quite a drop in replacement for SimpleDateFormat, but joda-time is much more concurrent friendly in terms of safety and performance.

    0 讨论(0)
提交回复
热议问题