Making DateFormat Threadsafe. What to use, synchronized or Thread local

前端 未结 6 1561
甜味超标
甜味超标 2020-12-17 22:14

I want to make following code thread safe. What is the best way to achieve it?

private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 22:47

    The simplest solution would be:

    synchronized public static final String eventTypeToDateTimeString(long timestamp) {
      return DATE_FORMAT.format(new Date(timestamp));
    }
    

    No need to use ThreadLocals since this is all in a static context.

提交回复
热议问题