ConcurrentHashMap迭代保证

时间:2015-10-17 20:07:59

标签: java multithreading synchronization concurrenthashmap

鉴于ConcurrentHashMap javadocs状态:

  

" Iterators和Enumerations返回反映状态的元素   在迭代器创建时或之后的某个时刻的哈希表"

我认为我保证在下面的示例中,一个或两个线程将调用fireAllFinished()。有没有一种情况下都不会调用fireAllFinished()?

java.lang.IllegalArgumentException: Item at position 0 requires 2 spans but GridLayoutManager has only 1 spans.
   at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:430)
   at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
   at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
   at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:14565)
   at android.view.ViewGroup.layout(ViewGroup.java:4473)
   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2185)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1999)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1212)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4981)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
   at android.view.Choreographer.doCallbacks(Choreographer.java:591)
   at android.view.Choreographer.doFrame(Choreographer.java:561)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5306)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(NativeStart.java)

(我省略了一些线程创建代码。我希望意图明确)

更新:我已经看到了这个更一般的问题:Is iterating ConcurrentHashMap values thread safe?,但想确认我的具体观点

  

"在某些时候"

在处理乱序运行代码的多核机器时,

通常是一个不精确的概念,两个线程可能同时更新地图的不同部分,并且设计没有办法锁定整个ConcurrentHashMap。

1 个答案:

答案 0 :(得分:2)

阅读ConcurrentHashMap ...

的文档
  

检索反映了最近完成的更新操作的结果。 (更正式地说,给定密钥的更新操作与报告更新值的该密钥的任何(非空)检索之前发生关系。)

  

对于诸如putAll和clear之类的聚合操作,并发检索可能反映仅插入或删除某些条目。类似地,Iterators,Spliterators和Enumerations在迭代器/枚举的创建时或之后的某个时刻返回反映哈希表状态的元素。

它没有措辞明确,但最近完成或之后应该意味着地图操作和迭代器创建是顺序一致的。< / p>

使用您的示例,如果我们调用map put A ,并且值检查 B ,则您有...

T1 A - &gt;的

T2 A - &gt;的

B 之前发生 ,但 T1 T2 同时发生。只要 A 发生在 B 之前,两个需要发生的某些有效序列的顺序一致意味着什么。但是, T1 T2 之间的任何排序都是有效的。

e.g。

T1:A - &gt; T1:B - &gt; T2:A - &gt;的 T2:乙

T1:A - &gt; T2:A - &gt; T2:B - &gt;的 T1:乙

因此,当代码实际运行时,可能会发生任何有效排序,但 T1:B T2:B (检查)必须最后发生。因此,fireAllFinished被调用一次或两次。线性同步会进一步限制所有事件之间的显式排序。

迭代整个地图可能有点贵,而且使用AtomicInteger或其他同步机制(如ConcurrentLinkedQueue)可能更简单。