首页  

jackson与SimpleDateFormat     所属分类 java 浏览量 80
SimpleDateFormat 非线程安全
为何 全局唯一的实例 ObjectMapper ,可以设置一个 SimpleDateFormat 实例


private static final ObjectMapper mapper = new ObjectMapper();
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
static{
    	// 
    	// public class SimpleDateFormat extends DateFormat
    	mapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT,Locale.US));
    	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

com.fasterxml.jackson.databind.SerializerProvider._dateFormat()

    protected final DateFormat _dateFormat()
    {
        if (_dateFormat != null) {
            return _dateFormat;
        }
        // At this point, all timezone configuration should have occurred,
        // with respect to default dateformat configuration. 
        // But we still better clone an instance as formatters are stateful, not thread-safe.
        
        DateFormat df = _config.getDateFormat();
        _dateFormat = df = (DateFormat) df.clone();
        // [databind#939]: 26-Sep-2015, tatu: With 2.6, formatter has been (pre)configured
        // with TimeZone, so we should NOT try overriding it unlike with earlier versions
        // 
        // TimeZone tz = getTimeZone();
        // if (tz != df.getTimeZone()) {
        //    df.setTimeZone(tz);
        // }
        
        return df;
    }
    
    
java.text.SimpleDateFormat.clone()

    @Override
    public Object clone() {
        SimpleDateFormat other = (SimpleDateFormat) super.clone();
        other.formatData = (DateFormatSymbols) formatData.clone();
        return other;
    }
  
SimpleDateFormat.clone 上打断点
    
序列化反序列化时间字段时 ,每次都会clone一个实例


Thread [main] (Suspended (breakpoint at line 2324 in SimpleDateFormat)) SimpleDateFormat.clone() line: 2324 DefaultSerializerProvider$Impl(SerializerProvider)._dateFormat() line: 1430 DefaultSerializerProvider$Impl(SerializerProvider).defaultSerializeDateValue(Date, JsonGenerator) line: 1090 DateSerializer(DateTimeSerializerBase<T>)._serializeAsString(Date, JsonGenerator, SerializerProvider) line: 212 DateSerializer.serialize(Date, JsonGenerator, SerializerProvider) line: 51 DateSerializer.serialize(Object, JsonGenerator, SerializerProvider) line: 15 BeanPropertyWriter.serializeAsField(Object, JsonGenerator, SerializerProvider) line: 727 BeanSerializer(BeanSerializerBase).serializeFields(Object, JsonGenerator, SerializerProvider) line: 719 BeanSerializer.serialize(Object, JsonGenerator, SerializerProvider) line: 155 DefaultSerializerProvider$Impl(DefaultSerializerProvider)._serialize(JsonGenerator, Object, JsonSerializer<Object>) line: 480 DefaultSerializerProvider$Impl(DefaultSerializerProvider).serializeValue(JsonGenerator, Object) line: 319 ObjectMapper._configAndWriteValue(JsonGenerator, Object) line: 3893 ObjectMapper.writeValueAsString(Object) line: 3207 JacksonUtil.toString(Object, boolean) line: 62 JacksonTest.main(String[]) line: 19 ========== Thread [main] (Suspended (breakpoint at line 2324 in SimpleDateFormat)) SimpleDateFormat.clone() line: 2324 DefaultDeserializationContext$Impl(DeserializationContext).getDateFormat() line: 1767 DefaultDeserializationContext$Impl(DeserializationContext).parseDate(String) line: 709 DateDeserializers$DateDeserializer(StdDeserializer<T>)._parseDate(String, DeserializationContext) line: 522 DateDeserializers$DateDeserializer(StdDeserializer<T>)._parseDate(JsonParser, DeserializationContext) line: 467 DateDeserializers$DateDeserializer(DateDeserializers$DateBasedDeserializer<T>)._parseDate(JsonParser, DeserializationContext) line: 195 DateDeserializers$DateDeserializer.deserialize(JsonParser, DeserializationContext) line: 285 DateDeserializers$DateDeserializer.deserialize(JsonParser, DeserializationContext) line: 268 FieldProperty.deserializeAndSet(JsonParser, DeserializationContext, Object) line: 136 BeanDeserializer.vanillaDeserialize(JsonParser, DeserializationContext, JsonToken) line: 288 BeanDeserializer.deserialize(JsonParser, DeserializationContext) line: 151 ObjectMapper._readMapAndClose(JsonParser, JavaType) line: 4001 ObjectMapper.readValue(String, Class<T>) line: 2992 JacksonUtil.toObject(String, Class<T>) line: 73 JacksonTest.main(String[]) line: 21
完整测试代码 https://gitee.com/dyyx/springboothello/blob/master/src/test/java/dyyx/JacksonTest.java
时间格式化性能对比 SimpleDateFormat并发问题实例演示

上一篇     下一篇
中国历史上的两个短命王朝

JDK默认GC

时间格式化性能对比

中国简史

网络流行语

linux 获取进程工作目录