日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Sun JDK: Hashmap.get非法使用的挂起分析

發(fā)布時間:2025/7/14 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sun JDK: Hashmap.get非法使用的挂起分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Sun JDK: Hashmap.get非法使用的掛起分析


from:http://sdh5724.javaeye.com/blog/619130 最近在查看生產環(huán)境時候, 發(fā)現(xiàn)了一個很奇怪的現(xiàn)象, 某個群集的一臺機器8個CPU 被100%吃完。 拿到Java的線程棧的時候, 滿滿一大片, 幾乎都停在了Hashmap.get/put 方法上。 剛開始, 我以為是velocity的bug(也的確是他的bug). 但是, 為什么會掛起來的呢, 令我很難明白。 Java stack代碼
  • "pool-2-thread-1"?prio=10?tid=0x007b0d88?nid=0x25?runnable?[0xb0bff000..0xb0c01688] ??
  • at?java.util.HashMap.put(HashMap.java:420) ??
  • at?org.apache.velocity.util.introspection.ClassMap$MethodCache.get(ClassMap.java:271) ??
  • at?org.apache.velocity.util.introspection.ClassMap.findMethod(ClassMap.java:102) ??
  • at?org.apache.velocity.util.introspection.IntrospectorBase.getMethod(IntrospectorBase.java:105) ??
  • at?org.apache.velocity.util.introspection.Introspector.getMethod(Introspector.java:94) ??
  • at?org.apache.velocity.runtime.parser.node.PropertyExecutor.discover(PropertyExecutor.java:118) ??
  • at?org.apache.velocity.runtime.parser.node.PropertyExecutor.<init>(PropertyExecutor.java:56) ??
  • at?org.apache.velocity.util.introspection.UberspectImpl.getPropertyGet(UberspectImpl.java:246)??
  • "pool-2-thread-1" prio=10 tid=0x007b0d88 nid=0x25 runnable [0xb0bff000..0xb0c01688] at java.util.HashMap.put(HashMap.java:420) at org.apache.velocity.util.introspection.ClassMap$MethodCache.get(ClassMap.java:271) at org.apache.velocity.util.introspection.ClassMap.findMethod(ClassMap.java:102) at org.apache.velocity.util.introspection.IntrospectorBase.getMethod(IntrospectorBase.java:105) at org.apache.velocity.util.introspection.Introspector.getMethod(Introspector.java:94) at org.apache.velocity.runtime.parser.node.PropertyExecutor.discover(PropertyExecutor.java:118) at org.apache.velocity.runtime.parser.node.PropertyExecutor.<init>(PropertyExecutor.java:56) at org.apache.velocity.util.introspection.UberspectImpl.getPropertyGet(UberspectImpl.java:246) ? 前陣子, 在查閱 JDK bug 庫的時候, 曾經看到過關于非正確使用Hashmap導致jvm的掛起 的問題。具體有如下解釋: This is a classic symptom of an incorrectly synchronized use of HashMap. Clearly, the submitters need to use a thread-safe HashMap. If they upgraded to Java 5, they could just use ConcurrentHashMap. If they can't do this yet, they can use either the pre-JSR166 version, or better, the unofficial backport as mentioned by Martin. If they can't do any of these, they can use Hashtable or synchhronizedMap wrappers, and live with poorer performance. In any case, it's not a JDK or JVM bug. 我們知道Hashmap不是讀寫線程安全的, 如果僅僅是全部只讀才是線程安全的。 這是JDK文檔的解釋: Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the? Collections.synchronizedMap?method. 雖然, 我們知道Hashmap在被并發(fā)讀寫使用的時候, 會跑出ConcurrentModificationException這個異常, 但是JDK文檔明確指出, 這個異常拋出是屬于 fail-fast 的一個設計方法, 目的是為了開發(fā)者能及早的意識到線程安全問題發(fā)生。 但是, 這個fail-fast不是一定會發(fā)生, 而是可能會發(fā)生的行為。 因此, 在一個不確定狀態(tài)下的下,jvm線程發(fā)生持續(xù)100%cpu行為是比較容易理解了(for (Entry<K,V> e = table[i]; e != null; e = e.next), 估計是這個代碼進了死循環(huán)的狀態(tài))。 我對velocity代碼比較熟悉,結合了棧的情況, 可以看到velocity錯誤的使用了Hashmap作為方法cache的數(shù)據(jù)結構, 在并發(fā)處理初始化模板的時候,把機器的CPU 100%吃完的機會是會發(fā)生的。我感覺這個問題還是比較容易發(fā)生, 在短短的1個星期, 就觀察到2次這個現(xiàn)象。 我查閱了下velocity的Bug列表, 果然有人報告過這個問題: https://issues.apache.org/jira/browse/VELOCITY-718? 雖然bug已經修復, 但是要修改使用velocity的版本令我很擔憂。 velocity在某些細節(jié)處理的兼容性上非常糟糕。 曾經升級過一次, 出現(xiàn)過無數(shù)的不兼容的行為。 這個bug的大部分因該出現(xiàn)系統(tǒng)的初始化階段, 要么系統(tǒng)起來, 要么系統(tǒng)啟動失敗。 基于修改的風險性, 很是糾結。

    轉載于:https://blog.51cto.com/tianya23/385007

    總結

    以上是生活随笔為你收集整理的Sun JDK: Hashmap.get非法使用的挂起分析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。