Synchronizing access to SimpleDateFormat

前端 未结 9 1489
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题