??????????????????JACKSON JSON???????????????????????齫ObjectMapper?????????????????Ч???????????????????????????????????????????????????????????????????????????????????

?????????д????

????Java????

public final class JacksonJsonMapper {
static volatile ObjectMapper objectMapper = null;
private JacksonJsonMapper(){}
public static ObjectMapper getInstance(){
if (objectMapper==null){
objectMapper = new ObjectMapper();
}
return objectMapper;
}
}

?????????????????????????????????е?????????????????????????

????1???????getInstance()????????????????synchronized??????

????2???????objectMapper.writeValueAsString(object)????????????????????????????????????????????е?????????????????????????????????????????

?????????????????????????????ObjectMapper???????????????????????????????????????????????е????????

????Jackson follows thread-safety rules typical for modern factory-based Java data format handlers (similar to what?? say?? Stax or JAXP implementations do). For example:

????Factories (ObjectMapper?? JsonFactory) are thread-safe once configured: so ensure that all configuration is done from a single thread?? and before instantiating anything with factory.

????Reader/writer instances (like JsonParser and JsonParser) are not thread-safe -- there is usually no need for them to be?? but if for some reason you need to access them from multiple threads?? external synchronization is needed

????All transformer objects (custom serializers?? deserializers) are expected to be stateless?? and thereby thread safe -- state has to be stored somewhere outside instances (in ThreadLocal or context objects passed in?? like DeserializationContext).

????????????????????????????o??????????????

????Java????

public final class JacksonJsonMapper {
static volatile ObjectMapper objectMapper = null;
private JacksonJsonMapper(){}
public static ObjectMapper getInstance(){
if (objectMapper==null){
synchronized (ObjectMapper.class) {
if (objectMapper==null){
objectMapper = new ObjectMapper();
}
}
}
return objectMapper;
}
}

?????????????????????? volatile ?????????????????????????????н????????????????д????????????????????synchronized ??????????