<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    關于使用JAVA單例的問題分析

      這個問題由最開始使用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).雅思代考 雅思答案

      第一個顧慮在看完下面這篇文章后,得到了解決方法:

      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 關鍵字的配合。

    posted on 2013-03-12 20:39 好不容易 閱讀(348) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 在线综合亚洲欧洲综合网站| 亚洲国产综合专区电影在线 | 亚洲最大中文字幕无码网站| 日本亚洲免费无线码| 色www永久免费| 亚洲色大成网站www永久一区| igao激情在线视频免费| 精品亚洲视频在线观看| ass亚洲**毛茸茸pics| 无码人妻精品一二三区免费| 亚洲精品无码不卡在线播HE| 免费国产在线视频| 亚洲视频免费在线播放| eeuss免费天堂影院| 亚洲中文字幕不卡无码| 亚洲AV无码AV男人的天堂不卡| 国产男女性潮高清免费网站| 特级毛片在线大全免费播放| 亚洲人成电影网站免费| 亚洲精品精华液一区二区| 在免费jizzjizz在线播| 亚洲国产视频久久| 亚洲VA综合VA国产产VA中| 国产日本亚洲一区二区三区| 99re6在线精品视频免费播放| 亚洲中久无码不卡永久在线观看| 99免费精品视频| 亚洲jizzjizz在线播放久| 国产18禁黄网站免费观看| 丝瓜app免费下载网址进入ios| 亚洲高清无码专区视频| 免费人成激情视频在线观看冫 | 国产自国产自愉自愉免费24区| 亚洲精品综合久久中文字幕 | 亚洲av无码一区二区三区人妖| 亚洲午夜无码AV毛片久久| 国产色无码精品视频免费| 亚洲一区二区三区久久| 亚洲AV伊人久久青青草原| 国产精品免费大片| 老湿机一区午夜精品免费福利|