新闻中心> 文章详情

Java单例问题解析_南京Java培训

2015年07月20日

  这个问题由最开始使用JACKSONJSON而衍生出来,因为官网上建议将ObjectMapper作为全局变量使用从而提高效率,所以,我们项目里面使用了单例,在使用单例的时候,我们无可厚非的考虑了资源在使用时是否要保证互斥的情况。南京Java培训


  最开始的写法:


  Java代码


  publicfinalclassJacksonJsonMapper{


  staticvolatileObjectMapperobjectMapper=null;


  privateJacksonJsonMapper(){}


  publicstaticObjectMappergetInstance(){


  if(objectMapper==null){


  objectMapper=newObjectMapper();


  }


  returnobjectMapper;


  }


  }


  在此期间,我考虑了两个问题,并与团队中的另外一个兄弟发生了激烈的讨论:


  1、在使用getInstance()方法的时候,是否要使用synchronized关键字。南京Java培训


  2、在使用objectMapper.writeValueAsString(object)时,因为此方法非静态方法,在此方法内是否会使用到对象自有的属性,而在并发的时候出现前者属性被后者覆盖的问题。


  后再看了源码后,排除了第二个顾虑,ObjectMapper是与线程绑定的,所以是线程安全的,并且也在官网的线程安全介绍中得到了证实。


  Jacksonfollowsthread-safetyrulestypicalformodernfactory-basedJavadataformathandlers(similartowhat,say,StaxorJAXPimplementationsdo).Forexample:


  Factories(ObjectMapper,JsonFactory)arethread-safeonceconfigured:soensurethatallconfigurationisdonefromasinglethread,andbeforeinstantiatinganythingwithfactory.


  Reader/writerinstances(likeJsonParserandJsonParser)arenotthread-safe--thereisusuallynoneedforthemtobe,butifforsomereasonyouneedtoaccessthemfrommultiplethreads,externalsynchronizationisneeded


  Alltransformerobjects(customserializers,deserializers)areexpectedtobestateless,andtherebythreadsafe--statehastobestoredsomewhereoutsideinstances(inThreadLocalorcontextobjectspassedin,likeDeserializationContext).


  第一个顾虑在看完下面这篇文章后,得到了解决方法:


  Java代码


  publicfinalclassJacksonJsonMapper{


  staticvolatileObjectMapperobjectMapper=null; 南京Java培训


  privateJacksonJsonMapper(){}


  publicstaticObjectMappergetInstance(){


  if(objectMapper==null){


  synchronized(ObjectMapper.class){


  if(objectMapper==null){


  objectMapper=newObjectMapper();


  }


  }


  }


  returnobjectMapper;


  }


  }


  文章中详细说明了关键字volatile是在读取所申明的对象时,会要从内存中进行同步,但是不会对写时起作用,所以,还是需要synchronized关键字的配合。学Java软件开发,来江苏万和计算机培训中心


上一篇下一篇
按时发顺丰

技术交流群

Java大数据交流群560819979    加入
Python技术交流群595083299    加入
Oracle技术交流群595119011    加入
Web前端技术交流群604697610    加入
Huawei技术交流群482919361    加入
Redhat技术交流群587875348    加入
UI设计技术交流群511649801    加入
Cisco技术交流群596886705    加入
IT运维技术交流群605888381    加入